var string = "This is a string";
var key = "Th";
var patt = new RegExp(/\b key /, "i");
if(patt.test(string)){
alert("true");
}
How can I escape \b so I can use the key variable?
Thanks in advance!
Don't use a regular expression literal when using the RegExp constructor.
When you want a \ in a string literal, you have to escape it as \\.
Here's the code :
var patt = new RegExp("\\b " + key + " ", "i");
/, they're what defines a regular expression literal. So no, you can't use them when you want to use a variable.
indexOfbe enough?(string.indexOf(key) > -1)? See developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/… or even developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/…