JavaScript RegExp dotAll Property
Last Updated :
05 Aug, 2025
The dotAll property in JavaScript regular expressions determines whether the 's' (dotAll) flag is enabled. This flag modifies the behaviour of the dot (.) metacharacter, allowing it to match any character, including line terminators like \n, \r, and others. Without the s flag, the dot (.) matches any character except line breaks.
The dotAll property is a read-only Boolean that reflects whether the s flag is active in a given regular expression.
JavaScript
// Regular expression without 's' flag
let regex1 = /a.b/;
console.log(regex1.dotAll);
console.log(regex1.test("a\nb"));
// Regular expression with 's' flag
let regex2 = /a.b/s;
console.log(regex2.dotAll);
console.log(regex2.test("a\nb"));
Outputfalse
false
true
true
- Without s flag: The dot (.) does not match line breaks, so "a\nb" fails to match.
- With s flag: The dot (.) matches line breaks, allowing "a\nb" to match successfully.
Syntax
regex.dotAll
Key Points
- Read-Only: Indicates whether the s flag is present in the regex.
- Enhanced Matching: When the s flag is set, the dot (.) matches all characters, including line terminators.
- Default Behavior: By default, dotAll is false unless explicitly enabled with the s flag.
Real-World Examples of the dotAll Property
1. Matching Multiline Strings
JavaScript
let s = "Hello\nWorld!";
let regex = /Hello.World/s;
console.log(regex.test(s));
With the s flag, the dot (.) matches the newline character, allowing the regex to match the entire "Hello\nWorld!" string.
2. Extracting Text Across Lines
JavaScript
let s = `Start
Middle
End`;
let regex = /Start.*End/s;
console.log(regex.test(s));
console.log(s.match(regex)[0]);
Outputtrue
Start
Middle
End
The .* pattern, combined with the s flag, matches everything from "Start" to "End", including newlines.
3. Simple Multiline Match
JavaScript
let s = "abc\ndef";
let regex = /a.c/s;
console.log(regex.test(s));
4. Finding Content Between Tags
JavaScript
let html = "<div>\n Content\n</div>";
let regex = /<div>.*<\/div>/s;
console.log(html.match(regex)[0]);
Output<div>
Content
</div>
5. Matching Entire Multiline Blocks
JavaScript
let log = `Error: Something went wrong.
Details:
Line: 42
File: app.js`;
let regex = /Error:.*?app\.js/s;
console.log(regex.test(log));
6. Comparing dotAll and Multiline (m) Flags
- s (dotAll): Allows . to match newlines.
- m (multiline): Modifies ^ and $ to match the start and end of each line.
JavaScript
let s = "line1\nline2";
// Using 's'
let sRegex = /.+/s;
console.log(sRegex.test(s));
// Using 'm'
let mRegex = /^line2/m;
console.log(mRegex.test(s));
Why Use the dotAll Property?
- Simplified Matching: Avoid the need for explicit patterns like [\s\S] to match line terminators.
- Cleaner Regex: Makes regular expressions more readable and concise for multiline data.
- Versatile Use: Ideal for processing logs, HTML, or any content with potential line breaks.
Recommended Links:
Explore
JavaScript Basics
Array & String
Function & Object
OOP
Asynchronous JavaScript
Exception Handling
DOM
Advanced Topics