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:
-
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):
Here,(?s)foo.*bar.*will match absolutely everything, including newlines, fromfootobar.
- In many regex flavors (PCRE, Python, .NET, Java), you can use an inline flag like
-
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.
- If your JavaScript environment supports the ES2018+ feature, you can write:
-
Use a “match all” character class like
[\s\S]or[^\0]- In older or more limited regex engines (including older JavaScript without the
sflag), you can manually force matching of newline characters by substituting.with[\s\S],[\d\D],[^\0], or similar. - For example:
This ensures you catch all characters—whitespace, non-whitespace, newlines, etc.—betweenfoo[\s\S]*barfooandbar.
- In older or more limited regex engines (including older JavaScript without the
-
Use a multiline vs. singleline distinction
- Don’t confuse the multiline
mflag (which affects^and$to match start/end of lines) with singleline (dotall) mode, which affects whether.matches line breaks.
- Don’t confuse the multiline
Examples in Common Environments
-
PCRE (e.g., many scripting languages):
(?s)foo.*baror
/foo.*bar/s(the
safter the delimiter sets singleline mode). -
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'
- 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.DOTALLin Python,sflag 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:
-
Grokking JavaScript Fundamentals
Dive into closures, prototypes, async patterns, and more—useful for writing robust, modern JS (including advanced regex usage). -
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.