Suppose we have a URL like '/mentor/33' as an example
To manipulate this, I use regex: buildRegex('/mentor/:id');
'/mentor/:id' is transformed into '/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
);
}
How can I extract just the value 33 from '[^/]'
If I implement this function
private matchRegex(routeLink: string, routeRegex: string) {
const match = routeLink.match(new RegExp(routeRegex, "g"));
return match?.[0];
}
then I end up with /mentor/33 instead of only 33. I am seeking a more generic solution.