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?
>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
}