JS: Regex Flag s (dotAll)

By Xah Lee. Date: .

Flag s (dotall)

(new in ECMAScript 2018)

Make the dot . also match newline characters. this means, the dot will match any UTF-16 code unit. (note, for characters outside Unicode Basic Multilingual Plane , it will only match the first part of the Unicode Surrogate Pair.)

to make the dot . truly match any unicode character, turn on both flag s and Flag v (unicodeSets) .)

// without flag s
console.assert(
 /A.B/.test("A\nB") === false,
);

// with flag s
console.assert(
 /A.B/s.test("A\nB"),
);
// example of flag s but not matching character outside Unicode Basic Multilingual Plane
console.assert(
 /a.b/s.test("a🦋b") === false,
);
// false, because dot only match a single 16-bit code unit. the emoji butterfly is 2 code units. it is outside Basic Multilingual Plane

console.assert(
 /./s.test("🦋"),
);
// true because dot matched the first part of the surrogate pair of the butterfly character
// make the dot . truly match any unicode character.
console.assert(
 /a.b/sv.test("a🦋b"),
);

RegExp.prototype.dotAll

property RegExp.prototype.dotAll value is true if flag s is set.

const rgx = /some/s;
console.assert(rgx.dotAll === true);