Jiman Sahariah
Jiman Sahariah

Reputation: 110

JavaScript RegExp including flags in pattern

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

Upvotes: 2

Views: 610

Answers (1)

Tushar
Tushar

Reputation: 87203

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])

Upvotes: 4

Related Questions