RegExp.prototype.dotAll
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;否则为 false。s 标志指示点特殊字符(.)应该额外匹配字符串中的以下行终止符(“换行符”)字符,而如果没有该标志,则不会匹配这些字符。
- 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 [ "😄" ]
同时使用 s 和 u 标志可以更直观地让点(.)匹配任何 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 |
浏览器兼容性
加载中…