Name: proposal-regexp-dotall-flag
Owner: Ecma TC39
Description: Proposal to add the `s` (`dotAll`) flag to regular expressions in ECMAScript.
Created: 2016-08-15 08:00:53.0
Updated: 2018-04-27 14:07:33.0
Pushed: 2018-01-19 04:47:07.0
Homepage: https://tc39.github.io/proposal-regexp-dotall-flag/
Size: 48
Language: HTML
GitHub Committers
User | Most Recent Commit | # Commits |
---|
Other Committers
User | Most Recent Commit | # Commits |
---|
s
(dotAll
) flag for regular expressionsThis proposal is at stage 4 of the TC39 process.
In regular expression patterns, the dot .
matches a single character, regardless of what character it is. In ECMAScript, there are two exceptions to this:
.
doesn?t match astral characters. Setting the u
(unicode
) flag fixes that..
doesn?t match line terminator characters.ECMAScript recognizes the following line terminator characters:
\n
)\r
)However, there are more characters that, depending on the use case, could be considered as newline characters:
\v
)\f
)This makes the current behavior of .
problematic:
The proposal you?re looking at right now addresses the latter issue.
Developers wishing to truly match any character, including these line terminator characters, cannot use .
:
.bar/.test('foo\nbar');
false
Instead, developers have to resort to cryptic workarounds like [\s\S]
or [^]
:
[^]bar/.test('foo\nbar');
true
Since the need to match any character is quite common, other regular expression engines support a mode in which .
matches any character, including line terminators.
DOTALL
or SINGLELINE
/s
modifiers.
(?s)
.
s
.
Note the established tradition of naming these modifiers s
(short for singleline
) and dotAll
.
One exception is Ruby, where the m
flag (Regexp::MULTILINE
) also enables dotAll
mode. Unfortunately, we cannot do the same thing for the m
flag in JavaScript without breaking backwards compatibility.
We propose the addition of a new s
flag for ECMAScript regular expressions that makes .
match any character, including line terminators.
.bar/s.test('foo\nbar');
true
t re = /foo.bar/s; // Or, `const re = new RegExp('foo.bar', 's');`.
est('foo\nbar');
true
otAll
true
lags
's'
The meaning of existing regular expression patterns isn?t affected by this proposal since the new s
flag is required to opt-in to the new behavior.
dotAll
mode affect multiline
mode?This question might come up since the s
flag stands for singleline
, which seems to contradict m
/ multiline
? except it doesn?t. This is a bit unfortunate, but we?re just following the established naming tradition in other regular expression engines. Picking any other flag name would only cause more confusion. The accessor name dotAll
gives a much better description of the flag?s effect. For this reason, we recommend referring to this mode as dotAll
mode rather than singleline
mode.
Both modes are independent and can be combined. multiline
mode only affects anchors, and dotAll
mode only affects .
.
When both the s
(dotAll
) and m
(multiline
) flags are set, .
matches any character while still allowing ^
and $
to match, respectively, just after and just before line terminators within the string.
{ dotAllFlag: true }
option enabled