The dot in RegExp doesn't match all characters in JavaScript

1 min read

This post is part of my Today I learned series in which I share all my learnings regarding web development.

I spoke at EnterJS a few days ago and Brian Terlson gave an excellent keynote on ES2017 and the future of JavaScript. The talk included also upcoming regular expression features that may be added to the spec in ES2017. One of these new features is the s or dotAll flag for regular expressions. So why do we need this?

It turns out that the . meta character in regular expressions is not actually matching all characters. You're surprised? I was, too. Let me show you some examples.

/a.b/.test('a\nb');     // false
/a.b/.test('a\rb');     // false
/a.b/.test('a\u2028b'); // false
/a.b/.test('a\u2029b'); // false

The problem with this is, that this is not what developers expect. Until Wednesday I was also expecting that . matches any character and this leads to surprises and can result in hard to spot bugs.

The spec proposal introduces a new /s flag which fixes this behavior. In case you want to learn more read the proposal. It explains the motivation and "fix" really well.

Edited. Mathias Bynens later shared that when the proposal is approved /./su matches any code point including line terminator symbols.

Edited. The s (dotall) flag made it into the feature set of EcmaScript 2018.

Load time