Matching newlines in JavaScript
20th September 2004
Just a quick note: the . character in a JavaScript regular expression will never match a newline character. If you want to match any character including newlines you can use the [\s\S] character class instead, which means “any character that’s either whitespace or not whitespace”.
This differs from both Python and Perl, where regular expression flags can be used to alter the behaviour of the . character (re.DOTALL and /s respectively).
This tip courtesy of the denizens of #javascript on Freenode.
More recent articles
- Highlights from my appearance on the Data Renegades podcast with CL Kao and Dori Wilson - 26th November 2025
- Claude Opus 4.5, and why evaluating new LLMs is increasingly difficult - 24th November 2025
- sqlite-utils 4.0a1 has several (minor) backwards incompatible changes - 24th November 2025