The dot in RegExp doesn’t match all characters in JavaScript (#tilPost)

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 …


This content originally appeared on Stefan Judis Web Development and was authored by Stefan Judis

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.


Reply to Stefan


This content originally appeared on Stefan Judis Web Development and was authored by Stefan Judis


Print Share Comment Cite Upload Translate
APA
Stefan Judis | Sciencx (2022-12-01T20:23:59+00:00) » The dot in RegExp doesn’t match all characters in JavaScript (#tilPost). Retrieved from https://www.scien.cx/2017/06/22/the-dot-in-regexp-doesnt-match-all-characters-in-javascript-tilpost/.
MLA
" » The dot in RegExp doesn’t match all characters in JavaScript (#tilPost)." Stefan Judis | Sciencx - Thursday June 22, 2017, https://www.scien.cx/2017/06/22/the-dot-in-regexp-doesnt-match-all-characters-in-javascript-tilpost/
HARVARD
Stefan Judis | Sciencx Thursday June 22, 2017 » The dot in RegExp doesn’t match all characters in JavaScript (#tilPost)., viewed 2022-12-01T20:23:59+00:00,<https://www.scien.cx/2017/06/22/the-dot-in-regexp-doesnt-match-all-characters-in-javascript-tilpost/>
VANCOUVER
Stefan Judis | Sciencx - » The dot in RegExp doesn’t match all characters in JavaScript (#tilPost). [Internet]. [Accessed 2022-12-01T20:23:59+00:00]. Available from: https://www.scien.cx/2017/06/22/the-dot-in-regexp-doesnt-match-all-characters-in-javascript-tilpost/
CHICAGO
" » The dot in RegExp doesn’t match all characters in JavaScript (#tilPost)." Stefan Judis | Sciencx - Accessed 2022-12-01T20:23:59+00:00. https://www.scien.cx/2017/06/22/the-dot-in-regexp-doesnt-match-all-characters-in-javascript-tilpost/
IEEE
" » The dot in RegExp doesn’t match all characters in JavaScript (#tilPost)." Stefan Judis | Sciencx [Online]. Available: https://www.scien.cx/2017/06/22/the-dot-in-regexp-doesnt-match-all-characters-in-javascript-tilpost/. [Accessed: 2022-12-01T20:23:59+00:00]
rf:citation
» The dot in RegExp doesn’t match all characters in JavaScript (#tilPost) | Stefan Judis | Sciencx | https://www.scien.cx/2017/06/22/the-dot-in-regexp-doesnt-match-all-characters-in-javascript-tilpost/ | 2022-12-01T20:23:59+00:00
https://github.com/addpipe/simple-recorderjs-demo