0

I am trying to match a URL that starts with /user/ and ends with any number and slash.

example:

/user/345/

I tried /user/\d{3}, but the interpreter gives me an error at \d.

if (request.url == '/') {
    absPath = './public/index.html';
    serveStatic(response, cache, absPath);
} else if (request.url.match(/user/\d{3})) {}

any suggestions?

2
  • 1
    My guess would be to use delimiters /. Which means you'll need to escape yours : /\/user\/\d+\// Commented Nov 16, 2013 at 21:06
  • Trailing slash - yes or no ? Commented Nov 16, 2013 at 22:48

3 Answers 3

1

Let's make trailing slash optional:

/\/user\/\d{3}\/?/

or

/\/user\/[0-9]{3}\/?/
Sign up to request clarification or add additional context in comments.

Comments

1

Try this:

var str = "/user/345/";
alert(str.match(/\/user\/\d+\//g) != null);

without the last slash:

alert(str.match(/\/user\/\d+/g) != null);

2 Comments

this worked but what if I wanted to match "/user/345"... without the slash?
Then just remove the final \/ from the regexp. Or add a question mark just after it if you want it to be optional.
0

You're missing a backslash

request.url.match("/user/\\d{3}")

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.