0

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

7
  • what is "path param" (that need to be replaced by '*') - how to 'recognize it' ? Commented May 11, 2018 at 15:17
  • how does 1 in /user/1 comply to the Member names SHOULD start and end with the characters “a-z” ? Commented May 11, 2018 at 15:19
  • @MatusDubrava Because 1 in this case is a parameter not a member of the API. Commented May 11, 2018 at 15:20
  • @MatusDubrava it doesn't cause, 1 is a path parameter, it shouldn't follow the rules, and I need to replace it Commented May 11, 2018 at 15:20
  • That's like the issue, I need to find resource parts that are not follow the rules and replace them Commented May 11, 2018 at 15:21

2 Answers 2

1

i am guessing you are looking for a regex to capture the UUID :

this should be working in javascript :

/[a-z][a-z0-9]+[-]+[a-z0-9-]+[a-z]/

I suppose a UUID Should have at least two words, so at least one "-"

let a = "/user/e09e4f9f-cfcd-4a23-a88a/info"

const match = a.match(/[a-z][a-z0-9]+[-]+[a-z0-9-]+[a-z]/)

console.log(match[0])

So for your code, it should be something like

 const escapeResourcePathParameters = resource => resource
  .substr(resource.startsWith('/') ? 1 : 0)
  .split('/')
  .reduce((url, member) => {
    // with  REGEX.JSONAPI_RESOURCE_MEMBER = /[a-z][a-z0-9]*[-]+[a-z0-9-]+[a-z]/
    return `${url}/${member.replace(REGEX.JSONAPI_RESOURCE_MEMBER, '*')}`;

 }, '');
Sign up to request clarification or add additional context in comments.

3 Comments

I don;t think so, the user part is just an example, it can be /info/user/1/list/5
and how .* handles the rules described above?
>I suppose a UUID Should have at least two words, so at least one "-" correct, but this doesn't work, I'll share a bit of code that parses the url
0

You could use look-arounds:

(?<=\/)[a-z][a-z0-9-]*[a-z](?=\/)

As noted in my comment, no need to use the anchors ^ nor $. Also escape slash \/. The regex wil match the pattern [a-z][a-z0-9-]*[a-z] only when surrounded by slashes.

/user/e09e4f9f-cfcd-4a23-a88f-b9f2f265167f/info # result:  /*/*/info
/user/e09e4f9f-cfcd-4a23-a88f-b9f2f265167f      # result:  /*/e09e4f9f-cfcd-4a23-a88f-b9f2f265167f
/user/1                                         # result: /*/1

To match UUIDs use:

(?<=\/)[a-z0-9-]{8}-(?:[a-z0-9-]{4}-){3}[a-z0-9-]{12}(?=\/)

The UUID format is described here: https://en.wikipedia.org/wiki/Universally_unique_identifier#Format

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.