- Published at
- Updated at
- Reading time
dotAll flag for regular expressions. So why do we need this?
Edit: The s (dotall) flag made it into the feature set of EcmaScript 2018.
It turns out that the
meta character in regular expressions is not 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 unexpected behavior is that it can result in hard to spot bugs. The "dotall" spec proposal introduces a new
/s flag which intents to fix this behavior.
/a.b/s.test('a\nb'); // true /a.b/s.test('a\rb'); // true /a.b/s.test('a\u2028b'); // true /a.b/s.test('a\u2029b'); // true
What's the browser support of the dotAll flag? 👇
Yes? Cool! You might want to check out Web Weekly for more quick learnings. The last edition went out 15 days ago.