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

Dynamic value with regexp gives null result

here is my code, where i am trying to get different values using regexp, but getting only null as result.

const str = '12345';

for(let i = 1; i <= 4; i++){
 let count = `\d{${i}}`;
 let reg = new RegExp(count, 'g');
 const match = str.match(reg);
 console.log(match); //getting null
}

giving the result only null. any one help me to understand the issue here?

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

>Solution :

The \ in the count regex string needs to be escaped with another \ from the looks of things:

const str = '12345';

for(let i = 1; i <= 4; i++){
 let count = `\\d{${i}}`;
 let reg = new RegExp(count, 'g');
 const match = str.match(reg);
 console.log(match); //getting null
}
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