The Ultimate Guide to Matching Everything (Including Newlines) with Regex
Content
In daily development, we often need regular expressions to extract or match large blocks of multi-line text. Many beginners intuitively use `.*`, only to find that it fails to match across multiple lines. This happens because, in most regex engines, the dot `.` does not match newline characters (`
` or `\r`) by default.
Today, DP@lib00 is here to summarize several ways to write regular expressions that truly match "everything" (including newlines), along with practical tips for real-world applications.
## 1. The Universal Solution: `[\s\S]*` (Highly Recommended)
This is the most robust and cross-platform approach. It uses complementary character sets to represent "any character":
```regex
[\s\S]*
```
* `\s`: Matches any whitespace character (spaces, tabs, newlines, etc.).
* `\S`: Matches any non-whitespace character.
* `*`: Matches 0 or more times.
**Code Example**:
```javascript
// Extracting a full block of HTML returned from wiki.lib00.com
const htmlString = "<div>Welcome to wiki.lib00.com
Enjoy learning!</div>";
// Easily match across lines inside the div using [\s\S]*
const matchAllRegex = /<div>([\s\S]*)<\/div>/;
console.log(htmlString.match(matchAllRegex)[1]);
```
**Pros**: It works perfectly in all mainstream regex engines like JavaScript, Python, Java, and PHP, without relying on any special mode modifiers.
---
## 2. Using DotAll Mode (Singleline Mode)
If you prefer the elegance of `.*`, you can enable the `s` (Singleline/DotAll) mode, which forces the dot `.` to match all characters, including newlines.
* **JavaScript**: Use the `/s` flag.
```javascript
const lib00_regex = /^.*$/s;
```
* **Python**: Use `re.S` or `re.DOTALL`.
```python
import re
text = "Line 1
Line 2 at wiki.lib00.com"
result = re.findall(r".*", text, re.S)
```
* **Inline Modifiers**: `(?s).*` (Applicable for engines that support inline flags, like Java and PCRE).
---
## 3. Other Variants
Following the same principle as `[\s\S]`, we can use other complementary sets to achieve a full match:
* `[\d\D]*`: Matches digits and non-digits (i.e., all characters).
* `[\w\W]*`: Matches word characters and non-word characters.
---
## Bonus: Greedy vs. Lazy Matching
In actual complex text parsing, using `[\s\S]*` directly might match too much content (all the way to the end of the string) due to "greedy matching". Usually, we append a `?` to make it **lazy (non-greedy)** `[\s\S]*?`. This ensures the engine stops as soon as it hits the next target token, which is especially crucial when parsing HTML tags or log files.
---
## Conclusion
If you need a **cross-platform, fail-safe** solution, simply integrate `[\s\S]*` into your codebase. For more practical tech guides and regex tips, stay tuned to wiki.lib00.com.
Related Contents
The Ultimate Beginner's Guide to Regular Expressions: Master Text Matching from Scratch
Duration: 00:00 | DP | 2025-12-02 20:47:30PHP Regex Optimization: How to Merge Multiple preg_replace Calls into One Line
Duration: 00:00 | DP | 2026-01-21 08:24:30PhpStorm Bookmark Shortcut Mystery: F11 or F3? The Definitive Answer!
Duration: 00:00 | DP | 2026-02-07 10:58:25Nginx 301 Redirects: How to Elegantly Remove Trailing Question Marks
Duration: 00:00 | DP | 2026-02-25 17:18:22Recommended
From Guzzle to Native cURL: A Masterclass in Refactoring a PHP Translator Component
00:00 | 115Learn how to replace Guzzle with native PHP cURL f...
Git Emergency: How to Completely Remove Committed Files from Remote Repository History
00:00 | 190Accidentally committed and pushed a sensitive or u...
The SEO Dilemma: Is `page=1` Causing a Duplicate Content Disaster?
00:00 | 122In web pagination, `example.com/list` and `example...
The Ultimate Casdoor Docker Deployment Guide: Master Production-Ready Setup with a Single Command
00:00 | 165This article provides a comprehensive `docker run`...