Click here to Skip to main content
15,437,139 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
My string contains
JavaScript
str = "Its a test string @[test_u](89) and @[test_v](91), lets try it."

And I am extracting
JavaScript
usernames = str.match(/([^"@\[])[^@\[]+?(?=\]\([1-9]+[0-9]*\))+/g); // ["test_u","test_v"]

And I need to extract id's in similar way and need
JavaScript
["89", "91"]

I have incomplete regex as
JavaScript
ids = str.match(/([^"(])[^@\(]+?(?=\))+/g);

Please suggest regex for extracting id's.
Thanks

What I have tried:

I tried with
JavaScript
ids = str.match(/([^"(])[^@\(]+?(?=\))+/g);
Posted
Updated 11-Oct-19 8:12am
v3

Try:
(?<=@\[.*?\]\()\d+

That will give you two matches: "89" and "91".
Your code can do what it likes with them from that point.

If you are goign to use Regexes, then get a copy of Expresso[^] - it's free, and it examines, generates, and tests Regular expressions.
 
Share this answer
 
Comments
Member 11748901 11-Oct-19 11:25am     CRLF
Thanks I just modified
(?<=@\[.+?\]\()\d+(?=\))+
OriginalGriff 11-Oct-19 11:27am    
You're welcome!
Just a few interesting links to help building and debugging RegEx.
Here is a link to RegEx documentation:
perlre - perldoc.perl.org[^]
Here is links to tools to help build RegEx and debug them:
.NET Regex Tester - Regex Storm[^]
Expresso Regular Expression Tool[^]
RegExr: Learn, Build, & Test RegEx[^]
Online regex tester and debugger: PHP, PCRE, Python, Golang and JavaScript[^]
This one show you the RegEx as a nice graph which is really helpful to understand what is doing a RegEx: Debuggex: Online visual regex tester. JavaScript, Python, and PCRE.[^]
This site also show the Regex in a nice graph but can't test what match the RegEx: Regexper[^]
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900