Last Updated : 11 Jul, 2025
The . metacharacter in JavaScript regular expressions matches any single character except for a newline (\n) or other line terminators, such as \r. It is widely used as a wildcard to represent "any character."
JavaScript
let regex = /c.t/;
let str1 = "cat";
let str2 = "cut";
console.log(regex.test(str1));
console.log(regex.test(str2));
The pattern c.t matches both "cat" and "cut" because the . matches any single character between c and t.
Syntax:let regex = /./;
let regex = /./;
let str = "Hello";
console.log(str.match(regex));
[ 'H', index: 0, input: 'Hello', groups: undefined ]
The . matches the first character, H.
2. Matching Characters in a Pattern JavaScript
let regex = /c.t/;
let str = "cat, cut, cot";
let matches = str.match(regex);
console.log(matches);
[ 'cat', index: 0, input: 'cat, cut, cot', groups: undefined ]
The . matches any single character between c and t.
3. Skipping Specific Characters JavaScript
let regex = /t..k/;
let str = "track, trick, tuck";
let matches = str.match(regex);
console.log(matches);
[ 'tuck', index: 14, input: 'track, trick, tuck', groups: undefined ]
The pattern t..k matches "track" and "trick" but skips "tuck" because it doesn't have exactly two characters between t and k.
4. Handling Optional Characters JavaScript
let regex = /a.b/;
let str = "a_b, acb, aab";
let matches = str.match(regex);
console.log(matches);
[ 'a_b', index: 0, input: 'a_b, acb, aab', groups: undefined ]
The . matches any character, so it allows for variations between a and b.
5. Matching Any Character in a Sentence JavaScript
let regex = /.a./g;
let str = "cat bat rat";
let matches = str.match(regex);
console.log(matches);
[ 'cat', 'bat', 'rat' ]
The . matches any character before and after a.
Combining with Quantifiers JavaScript
let regex = /.../g;
let str = "abcdef";
console.log(str.match(regex));
Match Any Three Characters, The . with {3} quantifies that exactly three characters should be matched at a time.
Common Patterns Using ./.{1}/
/.{3}/
/a.b/
/.+/gLimitations
The . metacharacter is one of the most powerful tools in JavaScript regex, enabling dynamic and versatile string matching.
Recommended Links:RetroSearch is an open source project built by @garambo | Open a GitHub Issue
Search and Browse the WWW like it's 1997 | Search results from DuckDuckGo
HTML:
3.2
| Encoding:
UTF-8
| Version:
0.7.4