Monday, May 20, 2024
 Popular · Latest · Hot · Upcoming
40
rated 0 times [  46] [ 6]  / answers: 1 / hits: 101969  / 14 Years ago, mon, april 26, 2010, 12:00:00

How to create regex pattern which is concatenate with variable, something like this:



var test =52;
var re = new RegExp(/b+test+b/);
alert('51,52,53'.match(re));


Thanks


More From » regex

 Answers
13
var re = new RegExp(/b+test+b/); 


b in a string literal is a backspace character. When putting a regex in a string literal you need one more round of escaping:



var re = new RegExp(\b+test+\b); 


(You also don't need the // in this context.)


[#96970] Friday, April 23, 2010, 14 Years  [reply] [flag answer]
Only authorized users can answer the question. Please sign in first, or register a free account.
zariahdiamondz

Total Points: 649
Total Questions: 109
Total Answers: 88

Location: Tajikistan
Member since Thu, Apr 14, 2022
2 Years ago
;