JS: Regex Flags

By Xah Lee. Date: . Last updated: .

What is regex flag

Flag i (ignoreCase)

Ignore case.

// demo regex flag i

console.assert(
 /a/i.test("ABC"),
);

console.assert(
 /a/.test("ABC") === false,
);

RegExp.prototype.ignoreCase

RegExp.prototype.ignoreCase is a Getter Property. Value is true if flag i is set, on the given regex object.

const rgx = /some/i;
console.assert(rgx.ignoreCase === true);

Flag m (multiline)

console.assert(
 /^B/.test("A\nB") === false,
);
console.assert(
 /^B/m.test("A\nB"),
);
console.assert(
 /a/m.multiline,
);

RegExp.prototype.multiline

RegExp.prototype.multiline is a Getter Property. Value is true if flag m is set, on the given regex object.

const rgx = /some/m;
console.assert(rgx.multiline === true);

Flag d (hasIndices)

show the begin and end positions of the matched string in result.

Flag g (global)

Get all occurrences.

Flag s (dotAll)

Make the dot . also match newline characters.

Flag u (unicode)

turn on unicode mode. e.g. supports searching by unicode char property \p{unicodeProperty} etc.

Flag v (unicodeSets)

The v flag is an upgrade to the u flag. It enables set notation in char class, and others.

Flag y (sticky)

Make the match start at the index .lastIndex.