The dot and newlines
The dot matches almost any character, but not a line break. Which characters it skips in JavaScript, Python and Perl, and the dotall flag and [\s\S] fix.
The dot and newlines
The player loads from youtube-nocookie.com when you press play. Nothing reaches YouTube before that.
Not started
What you'll learn
- The dot matches any single character except a line terminator. In JavaScript that means \n, \r, \u2028 and \u2029. In Python and Perl only \n is excluded.
- That is why .* stops at the end of a line. /<div>.*<\/div>/ fails on a tag that spans two lines.
- To let the dot cross lines use the s (dotall) flag in JavaScript, re.S or (?s) in Python, /s in Perl, or write [\s\S] or [^] in JavaScript.
- The dot matches one UTF-16 code unit without the u flag, so /^.$/ rejects an emoji that /^.$/u accepts.
- An unescaped dot is also a common bug: /1.5/ matches 1x5. Use \. for a literal dot. Inside a class [.] is already literal.
The wildcard with a blind spot
The earlier lessons gave you ways to say “one of these” and “none of these”. Sometimes you want the loosest statement of all: “a character, I don’t care which”. The dot does that. a.c matches abc, a-c and a c.
It also has a blind spot, and the reason is, as I understand it, practical. Regex tools grew up working line by line on text files. If . crossed line endings, a careless .* in a log search would swallow the rest of the file. So by default the dot matches any character except a line break.
What the dot refuses
I fed single characters to /./ in Node 26. The pair shows the default result, then the result with the s flag:
a:Y/Y space:Y/Y \n:-/Y \r:-/Y
:-/Y
:-/Y \t:Y/Y \v:Y/Y \u0085:Y/Y
In JavaScript the dot refuses four characters: \n, \r,
(line separator) and
(paragraph separator). Tab, vertical tab and \u0085 all matched. Python is narrower. re.search(".", c) matched \r,
and \x85 and refused only \n. Perl matched \r as well. A pattern that depends on how the dot treats a carriage return gives different answers in different languages, which matters for files with Windows line endings.
Where it bites: multi-line text
Take a tag that spans lines:
const html = "<div>\n hello\n</div>";
/<div>.*<\/div>/.test(html) // false
The pattern wants <div>, then anything, then </div>, and the “anything” can’t hold the newline after <div>. The same thing happens in grep, which works line by line: printf 'a\nb\n' | /usr/bin/grep -c 'a.b' printed 0. And sed -E 's/<div>.*<\/div>/X/' left a three-line input untouched, because sed looks at one line at a time, until I added N;N; to join the lines first.
There are three fixes in JavaScript:
/<div>.*<\/div>/s.test(html) // true the s flag
/<div>[\s\S]*<\/div>/.test(html) // true any whitespace or non-whitespace
/<div>[^]*<\/div>/.test(html) // true a class that excludes nothing
html.match(/<div>(.*)<\/div>/s)[1] // "\n hello\n"
The s flag makes the dot match everything and is called dotall. /./s.dotAll reports true and /./s.flags is "s". The two classes work because \s and \S together cover every character, and [^] is “not nothing”. Older code uses [\s\S] because the s flag arrived later in the language, in ES2018. Both work on Node 26. Without s, "a\nb".match(/.+/g) gives ['a', 'b'] and with s it gives ['a\nb'].
Other flavours
Python spells the flag re.S, or (?s) inside the pattern. In my test re.search(r"<div>.*</div>", html) was falsy and each of re.S, (?s) and [\s\S]* made it match. Perl’s /s works the same way: $h =~ /<div>.*<\/div>/ was false and /s made it true. In pcre2grep, matching across lines needs -M, and even then the dot still stops at a newline:
$ printf '<div>\n hello\n</div>\n' | pcre2grep -M -c '<div>.*</div>'
0
$ printf '<div>\n hello\n</div>\n' | pcre2grep -M -c '(?s)<div>.*</div>'
1
So -M lets the pattern see several lines, and (?s) lets the dot cross them.
Two more traps
The dot is not “any character” in the sense of a visible symbol. Without the u flag it matches one UTF-16 code unit, and an emoji takes two:
/^.$/.test("😀") // false
/^.$/u.test("😀") // true
"😀".length // 2
The later unicode code points lesson explains why.
And an unescaped dot is the favourite regex bug from the literals lesson: "1.5 1x5".match(/1.5/g) returned both, while /1\.5/g returned only 1.5. Reach for [^"] or \S when you mean something narrower than “anything”. The dot is convenient, and that is why it matches things you did not intend.
About the video
Koolac’s “RegEx Dot (.) Explained For Beginners” was published on 27 May 2025 and runs 9 minutes 28 seconds. Its timestamps are: Example 1: Basic Dot Usage, Dot vs. Newline: Why Doesn’t It Match?, How to Use re.DOTALL in Python, Wildcard Search with the Dot, Example 2, and Escaping the Dot: Matching a Literal . (Using .). That is a close match for this lesson, and it is the only video I found whose chapters mention newlines. It teaches in Python, so the JavaScript spellings (s flag, [\s\S]) are not in it as far as its chapters show. I couldn’t read a transcript, so I can’t say how thorough any chapter is. English per its title and description.
What I did not run
GNU grep, regex101, Ruby, Java and .NET. The claim that s arrived in ES2018 is from memory; I confirmed only that Node 26 supports it.
Next
The dot fits anywhere. The next lesson is about positions instead of characters: Anchors and line boundaries.
Check yourself
Why does /<div>.*<\/div>/ fail on '<div>\n hello\n</div>'?
The dot does not match the newline characters, so .* cannot cross the line breaks. Add the s flag, or use [\s\S]* in place of .*.
Which characters does the dot refuse in JavaScript, and which in Python?
JavaScript refuses \n, \r, \u2028 and \u2029. Python refuses only \n (my test showed \r, \u2028 and \x85 all matched).
What are three ways to match any character including a newline in JavaScript?
The s flag on the pattern, the class [\s\S], and the class [^], which excludes nothing. All three matched the two-line test string.
How do you match a literal dot between digits?
Escape it as \. in the pattern, for example /\d\.\d/. A bare dot also matches letters, so /1.5/ matches 1x5.
Notes
Notes are saved to your account.
Everything else on this page works without one — progress and bookmarks keep themselves in this browser and come with you when you sign in. Notes are the one thing worth not trusting to a browser you might clear.
Your notes could not be loaded.