The dotAll mode in regular expressions translates this sentence into English.
ECMAScript 9 (ES2018) introduced the dotAll
mode for regular expressions, addressing the limitation where the .
character could not match line terminators through the s
flag. This feature simplifies multiline text processing, particularly when handling template strings or text blocks containing line breaks.
Limitations of the Dot (.
) in Traditional Mode
In traditional regular expressions, the metacharacter .
matches any single character except line terminators (\n
, \r
, \u2028
, \u2029
). For example:
const regex = /a.b/;
console.log(regex.test('aXb')); // true
console.log(regex.test('a\nb')); // false
This design prevented .
from directly matching content across lines, forcing developers to use workarounds like [\s\S]
:
const regex = /a[\s\S]b/;
console.log(regex.test('a\nb')); // true
Syntax and Features of dotAll Mode
ES9 introduced the s
flag to enable dotAll
mode, allowing .
to match all characters, including line terminators:
const regex = /a.b/s;
console.log(regex.test('aXb')); // true
console.log(regex.test('a\nb')); // true
Detecting dotAll Mode in Regular Expressions
The flags
property can be used to check if a regular expression has the s
flag:
const regex = /./s;
console.log(regex.flags.includes('s')); // true
Practical Use Cases
Matching Multiline Template Strings
dotAll
mode significantly simplifies code when processing template strings with line breaks:
const text = `
Name: John
Age: 30
`;
const regex = /Name:\s+(.*)\s+Age:\s+(.*)/s;
const match = text.match(regex);
console.log(match[1], match[2]); // "John", "30"
Extracting HTML Tag Content
Matching HTML tag content that may contain line breaks:
const html = '<div>Line 1\nLine 2</div>';
const regex = /<div>(.*?)<\/div>/s;
console.log(html.match(regex)[1]); // "Line 1\nLine 2"
Interaction with Other Flags
Difference from the m
Flag
m
(multiline mode): Only alters the behavior of^
and$
s
(dotAll mode): Alters the behavior of.
They can be combined:
const regex = /^a.+b$/ms;
console.log(regex.test('aX\nYb')); // true
Compatibility with the u
Flag
The s
flag is fully compatible with Unicode mode (u
):
const regex = /./su;
console.log(regex.test('\u{1F600}')); // true
Performance Considerations
While dotAll
mode offers convenience, it may impact performance in extreme cases:
// Example that may cause catastrophic backtracking
const regex = /<.*>/s;
console.log(regex.test('<a>\n<b>\n<c>'));
For complex scenarios, it's recommended to use more precise patterns:
// More efficient alternative
const regex = /<[^>]+>/;
Browser and Runtime Support
Modern JavaScript environments widely support the s
flag:
// Feature detection
const isDotAllSupported = () => {
try {
new RegExp('.', 's');
return true;
} catch (e) {
return false;
}
};
Node.js has fully supported this feature since v10.0.0, and mainstream browsers implemented it in versions released after 2018.
本站部分内容来自互联网,一切版权均归源网站或源作者所有。
如果侵犯了你的权益请来信告知我们删除。邮箱:cc@cccx.cn
上一篇:正则表达式反向断言
下一篇:模板字符串限制转义序列