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

Find all matches in string

I’m using the regex to search for all instances of the string that matches Hello[n] pattern.

var str = 'Hello[0] hello[2] hell Welcome to JavaScript.';
var regex = /hello+/gi;
var result = str.match(regex);

The code above produces the following outcome.

[ 'Hello', 'hello' ]

I want to know how to modify my regex to produce the following result.

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

[ 'Hello[0]', 'hello[1]',..... ]

>Solution :

If you want to include the number, you’ve to change the Regex to hello\[\d+\]+.
Working example: https://regex101.com/r/Xtt6ds/1

So you get:

var str = 'Hello[0] hello[2] hell Welcome to JavaScript.';
var regex = /hello\[\d+\]+/gi;
var result = str.match(regex);
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