The Dot Metacharacter
You'll learn how the dot (.) acts as a wildcard in regular expressions, matching almost any character.
What the Dot Does
In regex, the dot (.) is a special metacharacter that matches any single character except for newline characters (\n). Think of it as a wildcard card in a deck—it can stand in for any letter, digit, symbol, or whitespace (except line breaks).
const pattern = /c.t/;
console.log(pattern.test("cat")); // true - dot matches 'a'
console.log(pattern.test("cot")); // true - dot matches 'o'
console.log(pattern.test("c9t")); // true - dot matches '9'
console.log(pattern.test("c@t")); // true - dot matches '@'
console.log(pattern.test("c t")); // true - dot matches space
console.log(pattern.test("ct")); // false - no character between c and t
The Newline Exception
By default, the dot does not match newline characters:
const text = "c\nt"; // 'c', newline, 't'
console.log(/c.t/.test(text)); // false - dot doesn't match \n
However, the s flag (introduced in ES2018, though not yet in your lesson sequence) can change this behavior in modern environments.
Escaping the Dot
If you need to match a literal period character, escape it with a backslash:
console.log(/3.14/.test("3.14")); // true
console.log(/3.14/.test("3X14")); // true - dot matches X!
console.log(/3\.14/.test("3.14")); // true
console.log(/3\.14/.test("3X14")); // false - escaped dot only matches period
Key Takeaway: The dot (.) matches any single character except newlines, making it a powerful wildcard—but remember to escape it (\.) when you need to match a literal period.