Updated: Matching line feeds with regular expressions works differently in every language

Posted . Visible to the public. Auto-destruct in 60 days
  • JavaScript RegExps support /s to make . match line feeds

Changes

  • Although regular expression syntax is 99% interchangeable between languages, keep this in mind:
  • - By default, the dot character (`"."`) does **not** match a line feed (newline, line break, `"\n"`) in any language.
  • - Some languages allow you to modify the behavior of a regular expression by appending a modifier to the pattern expression. E.g. `/foo/i` makes the pattern case-insensitive in many languages. Note however that some of these modifiers may not exist or mean entirely different things in different languages.
  • - Some languages have a modifier that makes the dot match line feeds.
  • Here is how to make the dot match line feeds in various languages:
  • Ruby
  • -----
  • You can make the dot match line feeds by using the `/m` modifier. If you come from other languages, do **not** use the `/s` modifier in Ruby. It [changes the RegExp to interpret text as Shift JIS encoded](http://www.tutorialspoint.com/ruby/ruby_regular_expressions.htm) which you probably don't want.
  • -Javascript
  • -----------
  • -There is **no** modifier to make the dot match line feeds. You need to write `[\s\S]`or `(.|\s)` to match any character including line feeds, carriage returns, etc. There's a [TC39 proposal](https://github.com/tc39/proposal-regexp-x-mode) that's stuck in Stage 1.
  • -While there is a `/m` modifier in Javascript, it only changes the meaning of `^` and `$`.
  • +## JavaScript
  • +
  • +Since ES2018, JavaScript supports the `/s` (dotAll) modifier, which makes `.` match line feeds and other line terminators.
  • +
  • +```js
  • +/foo.bar/s.test("foo\nbar") // true
  • +```
  • +
  • +For compatibility with older JavaScript engines, use `[\s\S]` instead.
  • +
  • +JavaScript also has an `/m` (multiline) modifier, but this only changes the meaning of `^` and `$`, not `.`.
  • +
  • Perl
  • ----
  • You can make the dot match line feeds by using the `/s` modifier. Note that there is also `/m`, which does something very different in Perl than in Ruby.
Profile picture of Henning Koch
Henning Koch
License
Source code in this card is licensed under the MIT License.
Posted by Henning Koch to makandra dev (2026-11-12 13:01)