Combining regular expressions in Javascript

The answer is yes! You have to initialize the variable under the RegExp class:

var lower = new RegExp(/--RegexCode--/);
var upper = new RegExp(/--RegexCode--/);

hence, regex can be dynamically created. After creation:

"sampleString".replace(/--whatever it should do--/);

Then you can combine them normally, yes.

var finalRe = new RegExp(lower.source + "|" + upper.source);

Leave a Comment