Course contentsShow
← JavaScript
Lesson 1725 of 2,72336. Regular ExpressionsFree lesson

Dot-All Mode with 's'

Making the dot (.) match newline characters using the 's' flag.

Dot-All Mode with 's'

You'll learn how to make the dot (.) metacharacter match everything, including newline characters.

The Problem with the Default Dot

You already know that the dot (.) matches any single character—but there's a catch. By default, it doesn't match newline characters (\n, \r). This can be frustrating when you're working with multi-line text and want to capture content that spans across lines.

const text = "line one\nline two";
const regex = /one.two/;

console.log(regex.test(text)); // false — the dot can't cross the newline

Even though "one" and "two" are in the text, the dot stops at the newline boundary.

Enter the 's' Flag: Dot-All Mode

The s flag (also called "dotAll" mode) changes this behavior. When you add it to your regex, the dot becomes truly universal—it matches any character, including newlines.

const text = "line one\nline two";
const regexWithS = /one.two/s;

console.log(regexWithS.test(text)); // true — the dot now matches the newline!

Think of it like this: the default dot is "line-aware," stopping at line breaks. The s flag makes it "line-blind," treating newlines as just another character.

Practical Example

const html = "<div>\n  <p>Content</p>\n</div>";
const pattern = /<div>.*<\/div>/s;

console.log(pattern.test(html)); // true — matches across multiple lines

Without the s flag, that pattern would fail because .* couldn't cross the newlines.

Key Takeaway: The s flag enables dot-all mode, allowing the . metacharacter to match newline characters, making it truly match any single character in your text.