The dot: any character except a line terminator
Outside a character class, the dot matches any single character except the four line terminators. The s flag removes that exception and the u flag makes it match a whole code point.
At a glance
- Syntax
- .
- Excludes by default
- \n, \r, U+2028 and U+2029
- With the s flag
- matches those four as well
- With the u flag
- matches one code point rather than one code unit
- Inside a character class
- [.] is a literal full stop, no escape needed
- Literal full stop
- \. outside a class
In detail
The default exclusion is the whole personality of the dot. It matches letters, digits, punctuation, spaces, tabs and control characters, and it refuses the four line terminators, which is what makes a dot based pattern line oriented without anyone having asked for that. A pattern that mysteriously stops working on the second line of input is almost always a dot pattern.
Under the u flag the dot counts in code points instead of UTF-16 code units, so an emoji becomes one dot rather than two. Without the flag, /^.$/ says an emoji is not a single character, and matching /./ against one returns a lone surrogate that will not render on its own.
Inside a character class the dot loses all of its meaning and becomes an ordinary full stop, so [.] and [\.] are the same class and neither needs an escape. Outside a class it needs one, and forgetting that is how a pattern for a version number or a file extension ends up matching far more than it should.
The wider point is that .* is rarely the right tool when there is a known terminator. A negated class such as [^"]* or [^>]* says what you actually mean, matches the same text, and does it without the backtracking that a greedy or lazy dot brings with it.
Examples
"a\nb".match(/a.b/); // null
"a\nb".match(/a.b/s)[0]; // "a\nb"
/^.$/.test("\u{1F600}"); // false, that is two code units
/^.$/u.test("\u{1F600}"); // true"3.14".replace(/./g, "x"); // "xxxx", every character replaced
"3.14".replace(/\./g, ","); // "3,14", only the full stop
/^[.]$/.test("."); // true, no escape needed inside a classThings that catch people out
- An unescaped dot in a version or filename pattern matches any character, so /1.2/ happily matches "1x2". Escape it as \. outside a character class.
- The dot excludes line terminators but a negated character class does not, so swapping .* for [^x]* also changes whether the pattern crosses lines.
- Without the u flag the dot matches half of an astral character. Slicing on that boundary produces a lone surrogate that no longer renders.
Try it: Regex Tester
Type a pattern and a test string, watch every match highlight as you go, read a plain English breakdown of each token, and check the numbered and named groups it captured.
Regex Tester
LOCAL · Browser-OnlyTest and debug regular expressions with live matching and highlights
Related Regex syntax
All regex syntax →The s flag: dotAll mode
What dotAll changes, why it has no effect at all on character classes, and the [\s\S] idiom that did the same job before the s flag existed.
\S: non whitespace, and the [\s\S] any character trick
\S is the complement of \s, which makes it a blank check in one character. Paired with \s it also gives the standard match anything class.
Escaping metacharacters: which ones, and where
Twelve characters need escaping outside a character class and only four inside one. User input needs an escape function before it goes near new RegExp.
[abc]: character sets and what loses its meaning inside
A bracket set matches one character from a list. Most metacharacters go literal inside it, and only four keep any special meaning at all.
[^abc]: negated character sets that quietly cross newlines
A negated class matches one character that is not in the set, including line terminators. That is why a quoted string pattern runs across lines.
[a-z] ranges: code point order and the A-z bug
Ranges inside a character class run by code point, which is why [A-z] quietly includes six punctuation characters and [a-Z] is a syntax error.
Tools for this
- Regex Tester Free online regex tester - test and debug regular expressions with live matching and highlights
- Regex Playground Regex playground with live match highlighting, named groups, find and replace, and shareable links