0

I'm trying to parse ID from string /path1/path2/something/id/7272/more/path/8282/category/3131 and the problem is that the string could be different all the time but the very part of /id/* is the same all the time. So I'm trying to get it with the following regex but it seems doesn't work.

new RegExp(/^./\//ig/^\d+$/)

After matching the result should be 7272.

2

2 Answers 2

2

Just use the /id/ part of the string for your matching too:

/\/id\/(\d+)/

var str = '/path1/path2/something/id/7272/more/path/8282/category/3131';
console.log(str.match(/\/id\/(\d+)/));

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

3 Comments

You need to specify the preceding \/ or this will also find /anotherid/666/
May belong to the usecase, yes. In his example this is no problem. I will extend the answer ... thx @Archer
No worries :-).
-3

Your regex should be "/id/(\\d+)"

Group: 1

10 Comments

well that looks a lot of easier.
@eisbehr Nope, it has no errors in this regex. Remember regex is independent on any languages. This is the original format of regex So let's say. When I use the above regex in Java i have to escape it as : "/id/(\\d+)" In javascript, it should be : /\/id\/(\d+)/
There's a Javscript tag. It's language specific.
@Archer I know, But I said "your regex" it means I wrote it in original syntax of regular expression
I'd personally delete this answer as it is misleading.
|

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.