Metacharacters and Wildcards: The Core Building Blocks
Metacharacters are special characters that carry meaning beyond their literal character value in regex patterns.
1. The Dot Wildcard (.)
The . (dot) matches any single character except a newline:
// Matches "cat", "bat", "hat", "mat", etc.
/c.t/.test("cat"); // true
/c.t/.test("cut"); // true
/c.t/.test("ct"); // false (no character between c and t)2. Shorthand Character Classes
Instead of manually typing character ranges, regex provides shorthand classes:
| Shorthand | Matches | Equivalent |
\d | Any digit (0-9) | [0-9] |
\D | Any non-digit | [^0-9] |
\w | Any word character | [a-zA-Z0-9_] |
\W | Any non-word character | [^a-zA-Z0-9_] |
\s | Any whitespace | [ \t\n\r\f] |
\S | Any non-whitespace | [^ \t\n\r\f] |
3. Practical Examples
const log = "2026-06-16 Error code: 500";
// Match a date pattern
log.match(/\d{4}-\d{2}-\d{2}/); // ["2026-06-16"]
// Match the status code (3 consecutive digits)
log.match(/\d{3}/); // ["500"]
// Match the word "Error" (only word characters)
log.match(/\w+/g); // ["2026", "06", "16", "Error", "code", "500"]Published on Last updated: