Jump to content

Dynamic value in pattern


sandy1028

Recommended Posts

You can't use variables in a regex literal. You need to create a new RexExp object to do it.

 

Example:

var hints = 'foobar';
var myRegExp = new RegExp(hints+"\\s=\\s'(.+?)';");

console.log(myRegExp); 
// output: /foobar\s=\s'(.+?)';/
Also note the necessity to double escape backslashes.

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.