Level 1: Foundations · Lesson 3 of 30
The dot .
The dot matches any single character except a line break. It is the first wildcard you learn and the easiest one to overuse.
The dot . is a wildcard. It matches exactly one character, whatever that character is: a letter, a digit, a space, a hyphen or a symbol. The pattern c.t matches cat, cot, cut, c7t and even c t, because the dot stands for any one character between c and t.
The key word is one. The dot never matches zero characters, and it never matches two. So c.t does not match ct, because there is nothing between the letters, and it does not match cart, because there are two characters between them. To allow more or fewer characters you combine the dot with a quantifier, covered in the next lesson.
By default the dot does not match a line break. In JavaScript, Python, PHP, Java, Go, C#, Rust and Ruby, a plain . stops at the newline character \n. This is usually what you want, because it keeps matches on a single line. Most languages offer a flag, often called dotall or single-line mode, that lets the dot match newlines too. Ruby is a notable exception in naming: its m flag is the one that makes the dot match newlines.
Because the dot matches anything, it can match more than you expect. The pattern file.txt matches file.txt, but it also matches file-txt and file_txt, because the dot accepts the hyphen and the underscore. If you really mean a literal dot, you need to escape it as \.. Inside a character set like [.] the dot is also literal.
A good habit is to use the dot only when you truly accept any character. If you know the position holds a digit, use \d. If you know it holds a letter or digit, use a character set. Specific patterns are easier to read, run faster and produce fewer surprise matches.
Still, the dot is handy for quick searches. If you are not sure whether a date uses slashes, hyphens or dots, 2024.01.15 matches all three styles. Just remember that it would also match 2024x01y15, so check your results.
Try it in the tester: write b.t and add words like bat, bit, bt and boat to the text. Predict which ones match before you look.
Live tester
4 matches (JavaScript engine)
cat cot cut c7t ct cart
Examples
Numbers
/1.3/g13 1x3 1-3 123
Matches: “1x3”, “1-3”, “123”
The dot accepts any middle character, so 1x3, 1-3 and 123 all match, but 13 does not.
Text
/h.t/ghat hit hot heat
Matches: “hat”, “hit”, “hot”
Exactly one character fits between h and t, so heat is not matched.
Codes
/A.1/gA-1 A_1 AB1 A1
Matches: “A-1”, “A_1”, “AB1”
The dot accepts the hyphen, the underscore and the letter B in the middle position.
Practice tasks
Your regex runs over the text with the global flag. It passes when it finds every required match and nothing that should be left out.
Task 1: Match three-letter words that start with b and end with t.
bat bet bit but bt
Task 2: Match an x, any single character, then a y. Do not match when there are two characters or none in between.
x1y x22y xy
Task 3: Match file followed by one separator character and txt, whatever the separator is.
file.txt filetxt file-txt