var str = 'TEST, STRING';
var regex = new RegExp('^(.*)('+str+')(.*)$/i');
console.log(regex);
Output
/^(.*)(TEST, STRING)(.*)$\/i/
But I need the following output:
/^(.*)(TEST, STRING)(.*)$\/i
var str = 'TEST, STRING';
var regex = new RegExp('^(.*)('+str+')(.*)$/i');
console.log(regex);
Output
/^(.*)(TEST, STRING)(.*)$\/i/
But I need the following output:
/^(.*)(TEST, STRING)(.*)$\/i
The flags should be the second parameter to the RegExp constructor.
new RegExp('^(.*)(' + str + ')(.*)$', 'i');
^ ^^^
The syntax of RegExp constructor is
new RegExp(pattern[, flags])
/^(.*)(TEST, STRING)(.*)$\/\/i/RegExp constructor is the pattern. To add flags, use second param.