【发布时间】:2023-01-07 16:11:44
【问题描述】:
例如“/mentor/33 是 url”
我用正则表达式来做到这一点: buildRegex('/mentor/:id');
'/mentor/:id' 变成 '/mentor/[^/]'
const PATH_PARAM = {
PREFIX: ':',
REGEX_IDENTIFIER: '/:[^/]+',
REGEX_REPLACER: '/[^/]+',
};
private buildRegex(path: string) {
return this.ensureLeadingSlash(path).replace(
new RegExp(PATH_PARAM.REGEX_IDENTIFIER, 'g'),
PATH_PARAM.REGEX_REPLACER
);
}
我怎样才能从 [^/] 取回 33,所以值?
如果我用这个
private matchRegex(routeLink: string, routeRegex: string) {
const match = routeLink.match(new RegExp(routeRegex, "g"));
return match?.[0];
}
然后我返回 /mentor/33 而不是 33。我正在寻找通用方法。
【问题讨论】:
标签: angular typescript