RegExp.prototype.dotAll
Baseline
Widely available
This feature is well established and works across many devices and browser versions. It’s been available across browsers since 2020年7月.
dotAll は RegExp インスタンスのアクセサープロパティで、正規表現で s フラグが使用されているかどうかを示します。
試してみましょう
const regex1 = /f.o/s;
console.log(regex1.dotAll);
// 予想される結果: true
const regex2 = /bar/;
console.log(regex2.dotAll);
// 予想される結果: false
解説
RegExp.prototype.dotAll の値は s フラグが使用されている場合は true、それ以外の場合は false です。s フラグは、ドット特殊文字 (.) が追加で行末記号 ("newline") 文字と一致することを示します。これ以外の場合は一致しません。
- U+000A LINE FEED (LF) (
\n) - U+000D CARRIAGE RETURN (CR) (
\r) - U+2028 LINE SEPARATOR
- U+2029 PARAGRAPH SEPARATOR
これは事実上、ドットが任意の UTF-16 コード単位に一致することを意味します。ただし、 Unicode 基本多言語面 (BMP) 外にある文字、いわゆるアストラル文字(アストラル文字はサロゲートペアで表され、 1 つではなく 2 つの . パターンでの一致が必要となります。
js
"😄".match(/(.)(.)/s);
// Array(3) [ "😄", "\ud83d", "\ude04" ]
u (unicode) フラグを使用すると、ドットがアストラル文字を単一文字として一致させることができます。
js
"😄".match(/./su);
// Array [ "😄" ]
なお、.* のようなパターンは、u フラグがなくても、より大きなコンテキストの一部としてアストラル文字を消費する能力があります。
js
"😄".match(/.*/s);
// Array [ "😄" ]
s フラグと u フラグを併用することで、ドットがより直感的な方法で任意の Unicode 文字に一致するようになります。
dotAll の設定アクセサーは undefined です。このプロパティを直接変更することはできません。
例
dotAll の使用
js
const str1 = "bar\nexample foo example";
const regex1 = /bar.example/s;
console.log(regex1.dotAll); // true
console.log(str1.replace(regex1, "")); // foo example
const str2 = "bar\nexample foo example";
const regex2 = /bar.example/;
console.log(regex2.dotAll); // false
console.log(str2.replace(regex2, ""));
// bar
// example foo example
仕様書
| Specification |
|---|
| ECMAScript® 2026 Language Specification # sec-get-regexp.prototype.dotAll |