1

I'm trying to test a location.pathname. I'm passing a path to a function with regex. The path can be:

/dashboard
/dashboard/orders

And location.pathname can be:

/dashboard
/dashboard/orders
/dashboard/orders/5ea5c95684c28d4b7ff688e1
/dashboard/orders/anyOtherId

What I want to do is when passing /dashboard/orders/5ea5c95684c28d4b7ff688e1 as location.pathname it matches only with the path /dashboard/orders and not with /dashboard.

Testing Regex in https://regexr.com/ I could make it work: \/dashboard\/*, then I tried to implement it in javascript:

const matcher = new RegExp(`${path}\/*`)
matcher.test(location.pathname)

However, when I pass a path = /dashboard/orders and location.pathname = /dashboard/orders/5ea5c95684c28d4b7ff688e1 it matches with /dashboard and /dashboard/orders. I need it to match only with /dashboard/orders. /dashboard must match only when path is /dashboard and location.pathname = /dashboard.

How can I achieve this?

Thanks in advance

Edit: To be more clear, I need it to return true when path is /dashboard/orders and location.pathname is /dashboard/orders or /dashboard/orders/5ea5c95684c28d4b7ff688e1.

But it must return false when path is /dashboard and location.pathname is /dashboard/orders or /dashboard/orders/5ea5c95684c28d4b7ff688e1

10
  • surley your pattern should be \/dashboard\/orders\/* then? Commented Apr 29, 2020 at 15:05
  • Why do you need regex in the first place? I dont really get what exactly is the use case. I'd split the pathname at each slash and work with the array Commented Apr 29, 2020 at 15:06
  • Or test with string.startsWith(substring [, position]); ref: techonthenet.com/js/string_startswith.php Commented Apr 29, 2020 at 15:08
  • @anubhava this works when location.pathname is /dashboard/orders/5ea5c95684c28d4b7ff688e1, but when it's just /dashboard/orders it matches with path /dashboard Commented Apr 29, 2020 at 15:08
  • 1
    Worked like a charm @anubhava. Please answer the question with this solution, so I can mark as the solution. Thanks! Commented Apr 29, 2020 at 15:45

2 Answers 2

1

You can do a .replace on location.pathname to remove everything after 2nd path component and the just compare with the path variable.

This solution should work for you:

path == location.pathname.replace(/^(\/dashboard\/[\w-]+)\/.*$/, '$1')

RegEx Demo of .replace

Sign up to request clarification or add additional context in comments.

Comments

0

Here, you can use two different regexes. One regex -

 "\/dashboard(\/)?$"

This will match "/dashboard" and "/dashboard/"

Second regex -

 "\/dashboard\/orders(\/[a-zA-Z0-9]*(\/)?)?$"

this will match

/dashboard/orders /dashboard/orders/5ea5c95684c28d4b7ff688e1 /dashboard/orders/anyOtherId /dashboard/orders/ /dashboard/orders/5ea5c95684c28d4b7ff688e1/ /dashboard/orders/anyOtherId/

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.