Logo

How do I match any character across multiple lines in a regular expression?

To match any character across multiple lines (i.e., make the . special character include newline characters \n, \r, etc.), you typically need to use one of the following techniques, depending on your regex engine or environment:

  1. Enable “dotall” (singleline) mode

    • In many regex flavors (PCRE, Python, .NET, Java), you can use an inline flag like (?s) at the start of your pattern or a singleline option that tells . to match newline characters as well.
    • For example (PCRE style):
      (?s)foo.*bar
      Here, .* will match absolutely everything, including newlines, from foo to bar.
  2. Use the s (dotAll) flag (in newer JavaScript, ES2018+)

    • If your JavaScript environment supports the ES2018+ feature, you can write:
      const regex = /foo.*bar/s; // The 's' flag means '.' will match newlines too
    • This allows the . to match newline characters as well.
  3. Use a “match all” character class like [\s\S] or [^\0]

    • In older or more limited regex engines (including older JavaScript without the s flag), you can manually force matching of newline characters by substituting . with [\s\S], [\d\D], [^\0], or similar.
    • For example:
      foo[\s\S]*bar
      This ensures you catch all characters—whitespace, non-whitespace, newlines, etc.—between foo and bar.
  4. Use a multiline vs. singleline distinction

    • Don’t confuse the multiline m flag (which affects ^ and $ to match start/end of lines) with singleline (dotall) mode, which affects whether . matches line breaks.

Examples in Common Environments

  1. PCRE (e.g., many scripting languages):

    (?s)foo.*bar

    or

    /foo.*bar/s

    (the s after the delimiter sets singleline mode).

  2. JavaScript (ES2018+):

    const pattern = /foo.*bar/s; const text = `foo

some line bar`; const match = text.match(pattern); console.log(match); // 'foo\nsome line\nbar'


3. **Older JavaScript** (without `s` flag):
```js
const pattern = /foo[\s\S]*bar/;
const text = `foo
some line
bar`;
const match = text.match(pattern);
console.log(match[0]);
// 'foo\nsome line\nbar'
  1. Python:
    import re text = "foo\nsome line\nbar" pattern = re.compile(r'foo.*bar', re.DOTALL) match = pattern.search(text) if match: print(match.group(0)) # 'foo\nsome line\nbar'

Key Takeaways

  • Dotall mode ((?s) in many engines, re.DOTALL in Python, s flag in modern JavaScript) modifies . so it matches newlines too.
  • If your environment doesn’t support dotall, use the “character class trick,” e.g. [\s\S], [\d\D], or [^\0].
  • The multiline (m) option is unrelated to whether . matches newlines; it affects how ^ and $ anchor to lines rather than the entire string.

Bonus: Strengthen Your Regex & Coding Interview Skills

If you want to go deeper into regular expressions, JavaScript (or other languages), or prepare for coding interviews, consider these DesignGurus.io resources:

  1. Grokking JavaScript Fundamentals
    Dive into closures, prototypes, async patterns, and more—useful for writing robust, modern JS (including advanced regex usage).

  2. Grokking the Coding Interview: Patterns for Coding Questions
    Learn pattern-based problem solving for technical interviews and daily dev tasks.

For personalized guidance, check out Mock Interviews:

Also, explore free educational content on the DesignGurus.io YouTube channel.

Conclusion: To make . match across multiple lines, enable dotall/singleline mode (e.g., (?s), /s flag, re.DOTALL) or replace . with [\s\S] if dotall mode isn’t available.

CONTRIBUTOR
TechGrind