New Regexp not working

I am trying to convert the following expression to a new Regexp () style:

http://jsfiddle.net/HDWBZ/

var Wyrazenie = /\btest[az]*/g; 

I am really confused by this and have no idea how to fix it. The following is what I did, but obviously this does not work.

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

The question also arises, what would it look like if instead of "test" I would use a variable?

+7
source share
1 answer

You should use this instead ...

 new RegExp("\\btest[az]*", "g"); 

... how \b will be interpolated into a single (inconclusive) character when the JavaScript parser works through the corresponding string literal. The solution is to avoid a slash.

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

+9
source

All Articles