JavaScript RegExp dotAll Property
Last Updated :
05 Dec, 2024
Improve
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.
// 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"));
Output
false 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
let s = "Hello\nWorld!";
let regex = /Hello.World/s;
console.log(regex.test(s));
Output
true
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
let s = `Start
Middle
End`;
let regex = /Start.*End/s;
console.log(regex.test(s));
console.log(s.match(regex)[0]);
Output
true Start Middle End
The .* pattern, combined with the s flag, matches everything from "Start" to "End", including newlines.
3. Simple Multiline Match
let s = "abc\ndef";
let regex = /a.c/s;
console.log(regex.test(s));
Output
true
4. Finding Content Between Tags
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
let log = `Error: Something went wrong.
Details:
Line: 42
File: app.js`;
let regex = /Error:.*?app\.js/s;
console.log(regex.test(log));
Output
true
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.
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));
Output
true true
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:
- JavaScript RegExp Complete Reference
- Javascript Cheat Sheet-A Basic guide to JavaScript
- JavaScript Tutorial