TExcelTAD
Home › Regular Expressions › The Dot

The Dot

The dot . is the most general metacharacter: it matches any single character except a line break. h.t matches hat, hit, hot, h t and even h#t.

regex · try it yourself
/h.t/g
hat hit h t h#t ht heat
4 matches
#1 "hat" at index 0
#2 "hit" at index 4
#3 "h t" at index 8
#4 "h#t" at index 12

ht has no character between h and t, and heat has two — the dot always stands for exactly one character.

What counts as a line break?

In JavaScript the dot refuses to match these line terminators:

Character Escape Name
U+000A \n line feed (Unix newline)
U+000D \r carriage return (part of Windows \r\n)

| U+2028 | 
 | line separator | | U+2029 | 
 | paragraph separator |

So .+ stops at the end of each line:

regex · try it yourself
/.+/g
first line
second line
2 matches
#1 "first line" at index 0
#2 "second line" at index 11

The s (dotAll) flag

With the s flag the dot matches line breaks too. Compare the same pattern with and without it:

regex · try it yourself
/start.*end/
start of the story
...and the end
No match
regex · try it yourself
/start.*end/s
start of the story
...and the end
1 match
#1 "start of the story\n...and the end" at index 0

Note: Python calls this flag re.DOTALL (or re.S); in Java it is Pattern.DOTALL; inline you can often write (?s) at the start of the pattern — but JavaScript does not support inline (?s).

The dot is usually too generous

Because the dot matches almost anything, patterns built on it tend to match more than you planned. A naive time pattern accepts garbage:

regex · try it yourself
/\d\d.\d\d/g
12:30  12.30  12x30  1234567
4 matches
#1 "12:30" at index 0
#2 "12.30" at index 7
#3 "12x30" at index 14
#4 "12345" at index 21

Replace the dot with what you actually allow — here a class of separators:

regex · try it yourself
/\d\d[:.]\d\d/g
12:30  12.30  12x30  1234567
2 matches
#1 "12:30" at index 0
#2 "12.30" at index 7

Tip: Treat . as a last resort. When you know the set of allowed characters, a class like [^,] or [\w-] is safer and often faster.

A dot is not a literal dot

Remember: to match a real full stop you write \. — or put it in a class, [.], where the dot is always literal.

js · try it yourself
const files = ["report.pdf", "reportXpdf", "notes.txt"];
for (const f of files) {
  console.log(f, /^\w+\.pdf$/.test(f), /^\w+.pdf$/.test(f));
}
report.pdf true true
reportXpdf false true
notes.txt false false

Test yourself

Without flags, which character does . NOT match in JavaScript?

  1. A space
  2. A tab
  3. A newline \n
  4. A digit

How many characters does a.c match between a and c?

  1. Zero or one
  2. Exactly one
  3. One or more
  4. Any number

Which pattern matches a literal dot?

  1. .
  2. [.]
  3. .?
  4. (.)