JavaScript – Use variable in string match
Although the match function doesn’t accept string literals as regex patterns, you can use the constructor of the RegExp object and pass that to the String.match function: var re = new RegExp(yyy, ‘g’); xxx.match(re); Any flags you need (such as /g) can go into the second parameter.