How to put variable in regular expression match?

You need to use the RegExp constructor instead of a regex literal.

var string = 'asdgghjjkhkh';
var string2 = 'a';
var regex = new RegExp( string2, 'g' );
string.match(regex);

If you didn’t need the global modifier, then you could just pass string2, and .match() will create the regex for you.

string.match( string2 );

Leave a Comment

Hata!: SQLSTATE[HY000] [1045] Access denied for user 'divattrend_liink'@'localhost' (using password: YES)