Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Create array of regex matches javascript

I want to create an array of matches for my regex expression in javascript.

It should look like: ['<:S_egirl:1023714296590499882>', '<a:eee:1023672889490284606>']
However, my code currently only makes an array with 1 item which includes all the matches.

The expression is: /<:.+:(\d+)>/gm

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

And my code is const parsed_emotes = [].concat(emotes.match(/<:.+:(\d+)>/gm)); console.log(parsed_emotes);

I’ve been stuck on this for hours, any help is appreciated.

>Solution :

Make your .+ non-greedy with ?. Compare the output of these two matches:

'<:hello:1234> <:hello:1234>'.match(/<:.+:(\d+)>/gm)
// Produces ['<:hello:1234> <:hello:1234>']

Vs a non-greedy match:

'<:hello:1234> <:hello:1234>'.match(/<:.+?:(\d+)>/gm)
// Produces ['<:hello:1234>', '<:hello:1234>']
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading