Please support this book: buy it or donate

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:

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

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

  1. > /^.$/s.test('\n')
  2. 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:

  1. > /^.$/.test('?')
  2. false

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

  1. > /^.$/u.test('?')
  2. true

Second, the dot does not match line terminator characters:

  1. > /^.$/.test('\n')
  2. 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”).

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

10.2.1. Line terminators recognized by ECMAScript

Line termators in ECMAScript affect:

  • The dot, in all regular expressions that don’t have the flag /s.
  • The anchors ^ and $ if the flag /m (multiline) is used. The following for characters are considered line terminators by ECMAScript:

  • U+000A LINE FEED (LF) (\n)

  • U+000D CARRIAGE RETURN (CR) (\r)
  • U+2028 LINE SEPARATOR
  • U+2029 PARAGRAPH SEPARATOR There are additionally some newline-ish characters that are not considered line terminators by ECMAScript:

  • U+000B VERTICAL TAB (\v)

  • U+000C FORM FEED (\f)
  • U+0085 NEXT LINE Those three characters are matched by the dot without a flag:
  1. > /^...$/.test('\v\f\u{0085}')
  2. true

10.3. The proposal

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

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

The long name of /s is dotAll:

  1. > /./s.dotAll
  2. true
  3. > /./s.flags
  4. 's'
  5. > new RegExp('.', 's').dotAll
  6. true
  7. > /./.dotAll
  8. false

10.3.1. dotAll vs. multiline

  • dotAll only affects the dot.
  • multiline only affects ^ and $.

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#, …).