JS: Regex Flags
What is regex flag
- Regex flags change the meaning of the regular expression, or behavior of Regex Functions.
- For how to add them, see JS: Create Regex Object.
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)
- Make the regex syntax
^match any newline beginning. (instead of the beginning of whole string) - Make the regex syntax
$match any newline end. (instead of the end of whole string)
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.