Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

new Regexp doesn't work [duplicate]

I'm trying to convert following expression to "new Regexp()" style:

http://jsfiddle.net/HDWBZ/

var Wyrazenie = /\btest[a-z]*/g;

I'm really confused with it and have no idea how to fix it. Below is what I've done but obviously it doesn't work.

var Wyraznie = new RegExp("\btest[a-z]*","g");

Also have a question how would it look if instead of "test" I would use variable?

like image 213
mik.ro Avatar asked Dec 19 '12 17:12

mik.ro


1 Answers

You should use this instead...

new RegExp("\\btest[a-z]*", "g");

... as \b will be interpolated into a single (slashless) character when JavaScript parser works through the corresponding string literal. The solution is to escape slash itself.

DEMO: http://jsfiddle.net/HDWBZ/1/

like image 144
VisioN Avatar answered Oct 14 '22 00:10

VisioN