Explain 'dotAll' flag for regular expressions in JavaScript

The dotAll flag returns true or false depending upon if the s flag has been set in the regular expression. The s flag enables "dotAll" mode, where the dot . metacharacter matches any character, including newlines.

What is the dotAll Flag?

By default, the dot . in regular expressions matches any character except newlines. When the s flag is used, the dot can also match newline characters (
, \r, etc.), making it truly match "any" character.

Syntax

regex.dotAll  // Returns true if 's' flag is set, false otherwise

Example: Checking dotAll Flag

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>dotAll Flag Example</title>
</head>
<body>
    <h1>dotAll Flag for Regular Expressions</h1>
    <div id="result" style="color: green; font-size: 18px; margin: 10px 0;"></div>
    <button onclick="checkDotAll()">Check dotAll Flag</button>

    <script>
        function checkDotAll() {
            const regexWithS = new RegExp("Hello", "s");
            const regexWithoutS = new RegExp("Hello");
            
            const result = document.getElementById("result");
            result.innerHTML = `
                <p>Regex with 's' flag - dotAll: ${regexWithS.dotAll}</p>
                <p>Regex without 's' flag - dotAll: ${regexWithoutS.dotAll}</p>
            `;
        }
    </script>
</body>
</html>

Example: Practical Usage with Newlines

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>dotAll Practical Example</title>
</head>
<body>
    <h1>dotAll Flag - Matching Newlines</h1>
    <div id="output" style="font-family: monospace; white-space: pre-line; margin: 10px 0;"></div>
    <button onclick="testNewlineMatching()">Test Newline Matching</button>

    <script>
        function testNewlineMatching() {
            const text = "Hello\nWorld";
            const regexNormal = /Hello.World/;
            const regexDotAll = /Hello.World/s;
            
            const output = document.getElementById("output");
            output.innerHTML = `
Text: "Hello\nWorld"

Normal regex /Hello.World/:
- Matches: ${regexNormal.test(text)}
- dotAll flag: ${regexNormal.dotAll}

DotAll regex /Hello.World/s:
- Matches: ${regexDotAll.test(text)}
- dotAll flag: ${regexDotAll.dotAll}
            `;
        }
    </script>
</body>
</html>

Comparison

Flag dotAll Property Dot Matches Newlines Example
Without 's' false No /Hello.World/
With 's' true Yes /Hello.World/s

Key Points

  • The dotAll property is read-only and returns a boolean
  • When true, the dot . matches any character including newlines
  • Useful for multiline text processing and pattern matching
  • Can also be set using new RegExp(pattern, "s") constructor

Conclusion

The dotAll flag is essential for detecting whether a regex can match newlines with the dot metacharacter. Use it to handle multiline text patterns effectively.

Updated on: 2026-03-15T23:18:59+05:30

308 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements