RegExp.prototype.dotAll

Baseline 已廣泛支援

此特性已經十分成熟,可在許多裝置和瀏覽器版本上使用。自 2020 年 7 月以來,它已在各大瀏覽器中可用。

RegExp 例項的 dotAll 訪問器屬性用於指示該正則表示式是否使用了 s 標誌。

試一試

const regex1 = /f.o/s;

console.log(regex1.dotAll);
// Expected output: true

const regex2 = /bar/;

console.log(regex2.dotAll);
// Expected output: false

描述

如果使用了 s 標誌,則 RegExp.prototype.dotAll 的值為 true;否則為 falses 標誌指示點特殊字元(.)應該額外匹配字串中的以下行終止符(“換行符”)字元,而如果沒有該標誌,則不會匹配這些字元。

  • U+000A LINE FEED (LF) (\n)
  • U+000D CARRIAGE RETURN (CR) (\r)
  • U+2028 LINE SEPARATOR
  • U+2029 PARAGRAPH SEPARATOR

這實際上意味著點(.)將匹配任何 UTF-16 碼單元。但是,它**不會**匹配超出 Unicode 基本多文種平面 (BMP) 的字元,也稱為增補字元,它們表示為 代理對,需要匹配兩個 . 模式而不是一個。

js
"😄".match(/(.)(.)/s);
// Array(3) [ "😄", "\ud83d", "\ude04" ]

可以透過使用 u (unicode) 標誌來允許點(.)將增補字元匹配為單個字元。

js
"😄".match(/./su);
// Array [ "😄" ]

請注意,即使沒有 u 標誌,像 .* 這樣的模式仍然有能力在更大的上下文中消耗增補字元。

js
"😄".match(/.*/s);
// Array [ "😄" ]

同時使用 su 標誌可以更直觀地讓點(.)匹配任何 Unicode 字元。

dotAll 的 setter 函式是 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

規範

規範
ECMAScript® 2026 語言規範
# sec-get-regexp.prototype.dotAll

瀏覽器相容性

另見