I have a bunch of regular expression, e.g. /[a-z]/
. Later in my program I need to have this as /[a-z]/g
,
so I need to add the 'global' modifier later. How can I add a modifier to an existing regular expression?

- 1,577
- 8
- 24

- 5,864
- 15
- 60
- 90
-
I believe you can access the modifiers like properties, such as `lower.global` or `lower.multiline` or `lower.ignoreCase`. – Chase Feb 24 '12 at 20:05
-
1@Chase They are read only [MDN ignoreCase](https://developer.mozilla.org/en/JavaScript/Reference/Global_Objects/RegExp/ignoreCase) – epascarello Feb 24 '12 at 20:08
-
@Chase, indeed, please make that an answer! – davin Feb 24 '12 at 20:12
-
@davin I'll defer to someone more experienced, I just tried it and it threw an error if I didn't call the toString() of the old one explicitly in the constructor. Might be more caveats, I'm sure someone else has been through this before. – Chase Feb 24 '12 at 20:15
-
@Chase, not sure how you're calling `toString()`, but you can also use `regex.source` – davin Feb 24 '12 at 20:17
-
@davin I figured toString would be called anyhow, but it apparently returns the slashes in the string, so that's no good. Thx for pointing out the source thing. – Chase Feb 24 '12 at 20:26
-
@Chase, I still urge you to make that into an answer, since it's your idea. Try and make a generic `modifyRegexFlags(oldRegex, newFlags)` function that simply reuses old flags unless new definitions are given. It's only a few lines of code! – davin Feb 24 '12 at 20:27
4 Answers
Use RegEx source and flags to separate the regular expression from the flags. Then create a new one with the string and set the needed flags.
var re = /^[a-z]*$/;
var re2 = new RegExp(re.source, re.flags + "i");
console.log( re.test("abc") )
console.log( re.test("ABC") )
console.log( re2.test("abc") )
console.log( re2.test("ABC") )

- 12,280
- 5
- 59
- 52

- 204,599
- 20
- 195
- 236
-
It's really awkward that it has to be done via string and even new regexp parsing... – Tomáš Zato Jan 24 '14 at 12:40
-
1Note that `flags` property was not implemented in internet explorer judging from mdn page. – Klesun Dec 03 '18 at 15:39
You can write a method for it-
RegExp.prototype.reflag= function(flags){
return RegExp(this.source, flags);
}

- 102,654
- 32
- 106
- 127
Here is a function to build on epascarello's answer and the comments. You said you have quite a few of regexps to modify later on, you could just redefine the variable they are referenced in or make some new ones with a function call.
function modifyRegexpFlags(old, mod) {
var newSrc = old.source;
mod = mod || "";
if (!mod) {
mod += (old.global) ? "g" : "";
mod += (old.ignoreCase) ? "i" : "";
mod += (old.multiline) ? "m" : "";
}
return new RegExp(newSrc, mod);
}
var lower = /[a-z]/;
//Some code in-between
lower = modifyRegexpFlags(lower, "g");
If the second argument is omitted, the old modifiers will be used.
(Credit to davin for the idea).

- 370
- 5
- 12
One aspect not really covered in earlier answers, so adding my two cents...
The excellent answers (+1 for epascarello!) here don't quite cover all the bases. If you want to generalize the function to allow any flags added to any regex:
function addregexflags(regx, newflags) {
// add new flags without duplication (won't work in old browsers or IE)
newflags = [...new Set([...regx.flags.split(''), ...newflags.split('')])].join('');
return new RegExp(regx.source, newflags);
}
addregexflags(/try/gi, "gm"); // /try/gim
If you have to support older browsers that don't support Sets and the spread operator, you need to longhand the union of strings as the RegExp constructor does not allow replication of flags.

- 1,297
- 9
- 16