Exploring ES2018 and ES2019
Please support this book: buy it or donate
(Ad, please don’t block.)

10. s (dotAll) flag for regular expressions



This chapter explains the proposal “s (dotAll) flag for regular expressions” by Mathias Bynens.

10.1. Overview

Currently, the dot (.) in regular expressions doesn’t match line terminator characters:

> /^.$/.test('\n')
false

The proposal specifies the regular expression flag /s that changes that:

> /^.$/s.test('\n')
true

10.2. Limitations of the dot (.) in regular expressions

The dot (.) in regular expressions has two limitations.

First, it doesn’t match astral (non-BMP) characters such as emoji:

> /^.$/.test('🙂')
false

This can be fixed via the /u (unicode) flag:

> /^.$/u.test('🙂')
true

Second, the dot does not match line terminator characters:

> /^.$/.test('\n')
false

That can currently only be fixed by replacing the dot with work-arounds such as [^] (“all characters except no character”) or [\s\S] (“either whitespace nor not whitespace”).

> /^[^]$/.test('\n')
true
> /^[\s\S]$/.test('\n')
true

10.2.1. Line terminators recognized by ECMAScript

Line termators in ECMAScript affect:

The following for characters are considered line terminators by ECMAScript:

There are additionally some newline-ish characters that are not considered line terminators by ECMAScript:

Those three characters are matched by the dot without a flag:

> /^...$/.test('\v\f\u{0085}')
true

10.3. The proposal

The proposal introduces the regular expression flag /s (short for “singleline”), which leads to the dot matching line terminators:

> /^.$/s.test('\n')
true

The long name of /s is dotAll:

> /./s.dotAll
true
> /./s.flags
's'
> new RegExp('.', 's').dotAll
true
> /./.dotAll
false

10.3.1. dotAll vs. multiline

10.4. FAQ

10.4.1. Why is the flag named /s?

dotAll is a good description of what the flag does, so, arguably, /a or /d would have been better names. However, /s is already an established name (Perl, Python, Java, C#, …).