1

Im trying to match a URL's path (window.location.pathname) but exclude anything further down the path.

I want to match the following:

  • /admin/sites/{2-6 digit number}{/ exclude the rest}

Examples

/admin/sites/123 - true

/admin/sites/1 - false

/admin/sites/123/foo - false

I've got as far as the following regex but can't seem to figure out the rest.

/admin\/sites\/[0-9]/.test(window.location.pathname)

2 Answers 2

3
/^\/admin\/sites\/\d{2,6}$/

the $ anchors the expression to the end of the string so it must end with the digits.

I also included the ^ so it must start with /admin.

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

Comments

2

If you want to match up to the / after the digits, you need the following regex:

^\/admin\/sites\/[0-9]{2,6}(?=\/)

See demo

1 Comment

Actually, I was in two minds since your question and provided code differ. In case you need more help, feel free to drop a comment.

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.