I am trying to execute a regular expression with a variable as the query.
//This works
$('body *').replaceText(/\b(Toronto)/gi, nameWrapper );
I need to have “Toronto” in a variable
var query = "Toronto";
$('body *').replaceText(/\b( -- query VARIABLE HERE -- )/gi, nameWrapper );
You need to use
RegExpto build a regular expression from a string:And to quote your string properly, you can use this:
Then just use
RegExp.quote(query)instead ofquerywhen building the regular expression: