JavaScript RegExp . Metacharacter
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."
let regex = /c.t/;
let str1 = "cat";
let str2 = "cut";
console.log(regex.test(str1));
console.log(regex.test(str2));
Output
true true
The pattern c.t matches both "cat" and "cut" because the . matches any single character between c and t.
Syntax:
let regex = /./;
- .: Matches any single character except a newline (\n) or line terminators (\r, \u2028, \u2029).
Key Points
- Wildcard: Represents "any character."
- Exceptions: Does not match line terminators like \n or \r.
- Flexibility: Often used in patterns where the exact character is variable.
Real-World Examples
1. Matching Any Character
let regex = /./;
let str = "Hello";
console.log(str.match(regex));
Output
[ 'H', index: 0, input: 'Hello', groups: undefined ]
The . matches the first character, H.
2. Matching Characters in a Pattern
let regex = /c.t/;
let str = "cat, cut, cot";
let matches = str.match(regex);
console.log(matches);
Output
[ 'cat', index: 0, input: 'cat, cut, cot', groups: undefined ]
The . matches any single character between c and t.
3. Skipping Specific Characters
let regex = /t..k/;
let str = "track, trick, tuck";
let matches = str.match(regex);
console.log(matches);
Output
[ '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
let regex = /a.b/;
let str = "a_b, acb, aab";
let matches = str.match(regex);
console.log(matches);
Output
[ '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
let regex = /.a./g;
let str = "cat bat rat";
let matches = str.match(regex);
console.log(matches);
Output
[ 'cat', 'bat', 'rat' ]
The . matches any character before and after a.
Combining with Quantifiers
let regex = /.../g;
let str = "abcdef";
console.log(str.match(regex));
Output
[ 'abc', 'def' ]
Match Any Three Characters, The . with {3} quantifies that exactly three characters should be matched at a time.
Common Patterns Using .
- Match Any Character Once:
/.{1}/
- Match Any Three Characters:
/.{3}/
- Match Any Character Before or After Specific Text:
/a.b/
- Match All Characters Except Newlines:
/.+/g
Limitations
- Does Not Match Newlines: The . cannot match \n, \r, or other line terminators.
- Specificity: For precise matching, consider combining . with other characters or patterns.
Why Use . Metacharacter?
- Wildcard Matching: Ideal for flexible pattern matching when exact characters are unknown.
- Versatile: Works in diverse scenarios, from parsing text to validating input.
- Combines Well: Can be paired with quantifiers, anchors, and other patterns for more complex matching.
Conclusion
The . metacharacter is one of the most powerful tools in JavaScript regex, enabling dynamic and versatile string matching.
Recommended Links:
- JavaScript RegExp Complete Reference
- JavaScript Cheat Sheet-A Basic guide to JavaScript
- JavaScript Tutorial