CKTIS / JavaScript
Regular expression flags: dotAll (`s`), `u`, `i`, and `g`
The `s`, `u`, `i`, and `g` flags control dot matching across line breaks, Unicode-aware matching, case-insensitive matching, and global search, respectively.
Version: ECMAScript 2024 and the current web platform; runtime support varies
Syntax
/pattern/gsui
new RegExp("pattern", "gsui")
re.dotAll; re.unicode; re.ignoreCase; re.global; re.lastIndexPut flags after the closing slash of a literal (/a.b/gsui) or in the second constructor argument (new RegExp("a.b", "gsui")). Flag order does not matter; an unknown or repeated flag causes a SyntaxError. The properties dotAll, unicode, ignoreCase, and global report whether s, u, i, and g are set; they do not change the flags. These rules apply in ECMAScript 2024, but runtime support varies.
s (dotAll) lets the wildcard . also match the four line terminators: \n, \r, U+2028, and U+2029. It does not change the ^ and $ anchors; m is what lets those also match line boundaries. An escaped dot, \., is still literal.
u enables Unicode-aware mode: a dot treats a valid surrogate pair such as 😄 as one code point rather than two UTF-16 code units. A code point is not necessarily a whole grapheme, or user-perceived character. u also enforces stricter syntax and cannot be combined with v (SyntaxError). i ignores case; with u, it uses Unicode simple case folding, which is locale-independent and does not expand ß to ss.
g enables iteration over non-overlapping matches. With g and no y, built-in exec() uses lastIndex as a UTF-16 offset to determine where to begin searching. In Unicode-aware mode, if that offset falls inside a valid surrogate pair, a match can start at the beginning of the pair, before lastIndex. On success, exec() stores the end of the match in lastIndex as a UTF-16 offset; on failure, it resets lastIndex to 0. test() can also change this state, so reset lastIndex or use a fresh RegExp for an independent search. An empty match can stall an exec() loop. String.prototype.matchAll() requires g for a RegExp argument (TypeError without it), handles advancement past empty matches, and iterates on a copy whose initial lastIndex is set from the original's current lastIndex.
Examples
{
const text = "a\nb A😄b";
const re = /a.b/gsui;
const collect = (pattern) => Array.from(text.matchAll(pattern), (m) => m[0]);
console.log(JSON.stringify(collect(re)));
console.log(JSON.stringify([/a.b/gui, /a.b/gsi, /a.b/gsu].map(collect)));
const positions = [];
for (let n = 0; n < 3; n++) {
re.exec(text);
positions.push(re.lastIndex);
}
console.log(JSON.stringify(positions));
}Result
["a\nb","A😄b"] [["A😄b"],["a\nb"],["a\nb"]] [3,8,0]
The first line shows both matches. The second drops `s`, `u`, and `i` in turn, each time from the full flag set: the match across the line break, the match with the emoji, or the match with the uppercase letter disappears. The third line shows `lastIndex` after two successes and one failure.
Related lessons
- JavaScript · Lesson 10 · Advanced · Version: 4