Context
I have code that takes an url path and replaces path params with '*'. All my urls follow JSON API naming convention. All valid url resource parts follow next rules:
- Member names SHOULD start and end with the characters “a-z” (U+0061 to U+007A)
- Member names SHOULD contain only the characters “a-z” (U+0061 to U+007A), “0-9” (U+0030 to U+0039), and the hyphen minus (U+002D HYPHEN-MINUS, “-“) as separator between multiple words.
The pass param usually is an id (number, uuid, guid, etc).
Here are several examples of transformations:
/user/e09e4f9f-cfcd-4a23-a88f-b9f2f265167f/info -> /user/*/info/user/e09e4f9f-cfcd-4a23-a88f-b9f2f265167f -> /user/*/user/1 -> /user/*
What I have
/^[a-z][a-z0-9-]*[a-z]$/
The issues is that it doesn't handle uuid as a path param.
Here is my function that parses the url (sorry don't have time to create a jsfiddle):
const escapeResourcePathParameters = resource => resource
.substr(resource.startsWith('/') ? 1 : 0)
.split('/')
.reduce((url, member) => {
const match = member.match(REGEX.JSONAPI_RESOURCE_MEMBER);
const part = match
? member
: '*';
return `${url}/${part}`;
}, '');
Questions
I need a regex that follows the rules above and works for the examples above.
UPD:
I've added my function that I use to parse urls. To test your regex, just replace it with REGEX.JSONAPI_RESOURCE_MEMBER and pass the url like
/user/e09e4f9f-cfcd-4a23-a88f-b9f2f265167f/info, it should return /user/*/info
1in/user/1comply to theMember names SHOULD start and end with the characters “a-z”?1in this case is a parameter not a member of the API.