0

I am trying to parse some ids out of a url and my original solution pulls the first id (my assumption that there would be only one). But now, I have come to learn that there could be more, so some changes to my original solution are needed, but I cannot seem to get it right.

Here are my basic requirements:

Return all numbers between '/' or '=' and '&' or '/' or end of string.

My original requirements:

Return numbers found between '/' or '=' and '&'

Here is my original solution:

([^\/|=]*)(\&)

Here is an example url that I have been using:

https://www.website.com/blah?blah_id=1234567890&id=0987654321

I want to return 1234567890 and 0987654321

I have tried several solutions, none of which have worked for me, so please do not mark as a duplicate.

johnny_mac
  • 1,801
  • 3
  • 20
  • 48
  • you can do something like this `var id = url.match(/[&\?]id=(\d+)([^0-9]|$)/)[1]`. I'm not sure if the regex covers all possibilities but it should work for most of them. – Titus Apr 12 '17 at 16:28
  • @Titus thanks for responding. I have tried a variation of this, and no joy. I also just tried your straight up suggestion in case I was wrong anyway, and it returns the last id, as my attempt did. – johnny_mac Apr 12 '17 at 16:31
  • Now I see that you're trying to retrieve both IDs, you'll have to run that code twice, once with `[&\?]id=(\d+)([^0-9]|$)` and the second time with `[&\?]blah_id=(\d+)([^0-9]|$)` – Titus Apr 12 '17 at 16:38
  • Hmm, that is not what I thought. Can I not get all matches of id= in the string? – johnny_mac Apr 12 '17 at 16:43
  • I guess you can do that using something like this `var idsArray = url.match(/id=(\d+)/g).map(v => v.replace(/(blah_)?id=/, ""))` but the regex here is kind of unreliable. – Titus Apr 12 '17 at 16:51

0 Answers0