0

I have this code:

if(/^\/[^/]+\/[^/]+\/collections/.test(location.pathname)) { alert("ok")};

This match with:

http://localhost:3000/es/hyperrjas/collections

The problem is that also match with:

http://localhost:3000/es/hyperrjas/collections/thing1
http://localhost:3000/es/hyperrjas/collections/thing2
http://localhost:3000/es/hyperrjas/collections/thing3
.
.
.

I need only match with the first type url:

http://localhost:3000/es/hyperrjas/collections
http://localhost:3000/es/michael-25/collections
http://localhost:3000/es/other_username/collections

I don't want match the urls with thing1, thing2, thing3....etc

How can I fix this regex javascript?

0

1 Answer 1

2

Add on the delimiter $ to the end of the regular expression


Your new expression would look like this:

/^\/[^/]+\/[^/]+\/collections$/
Sign up to request clarification or add additional context in comments.

1 Comment

THank you can you write how does works with this regexp, if(/^\/[^/]+\/[^/]+\/collections/.test(location.pathname)) { alert("ok")};

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.