J

JavaScript Handbook

Clean • Professional

RegEx Metacharacters in JavaScript

3 minute

Regular Expression Metacharacters

Metacharacters are special symbols in JavaScript regular expressions that control pattern matching behavior. They allow you to match patterns beyond literal characters.

1. Common Metacharacters

. (Dot)

  • Matches any single character except newline (\\n).
  • With the s flag, it also matches newlines.
console.log("cat".match(/c.t/));    // ["cat"]
console.log("c\\nt".match(/c.t/s));  // ["c\\nt"]

^ (Caret)

  • Matches the start of a string.
  • In multiline (m) mode, matches the start of each line.
console.log(/^hello/.test("hello world")); // true
console.log(/^world/m.test("hello\\nworld")); // true

$ (Dollar)

  • Matches the end of a string.
  • In multiline mode, matches the end of each line.
console.log(/world$/.test("hello world"));  // true
console.log(/hello$/m.test("hello\\nworld")); // true

(Asterisk)

Matches 0 or more occurrences of the preceding character or group.

console.log("ct".match(/ca*t/));    // ["ct"]
console.log("caat".match(/ca*t/));  // ["caat"]

+ (Plus)

Matches 1 or more occurrences of the preceding character or group.

console.log("ct".match(/ca+t/));    // null
console.log("caat".match(/ca+t/));  // ["caat"]

? (Question Mark)

  • Matches 0 or 1 occurrence of the preceding character or group.
  • Also used for non-greedy matching.
console.log("cat".match(/ca?t/)); // ["cat"]
console.log("ct".match(/ca?t/));  // ["ct"]

| (Pipe)

Acts as an OR operator, matching either the pattern before or after it.

console.log("cat".match(/cat|dog/)); // ["cat"]
console.log("dog".match(/cat|dog/)); // ["dog"]

() (Parentheses)

Groups patterns together and can capture matched substrings for later use.

console.log("abc".match(/(ab)c/)); // ["abc", "ab"]
console.log("abc abc".replace(/(ab)c/g, "$1d")); // "abd abd"

[] (Square Brackets)

Defines a character class, matching any single character inside the brackets.

console.log("cat".match(/[a-c]/g)); // ["c", "a"]

{} (Curly Braces)

Specifies number of occurrences of the preceding character or group:

  • {n} → exactly n times
  • {n,} → at least n times
  • {n,m} → between n and m times
console.log("caaat".match(/ca{2,4}t/)); // ["caaat"]

\ (Backslash)

  • Escapes a metacharacter to match it literally.
  • Also introduces special sequences like \\d, \\w, etc.
console.log("a.b".match(/a\\.b/));  // ["a.b"]
console.log("123".match(/\\d+/));   // ["123"]

\b (Word Boundary)

Matches the position between a word character (\\w) and a non-word character (\\W).

console.log("hello world".match(/\\bworld\\b/)); // ["world"]

\B (Non-Word Boundary)

Matches a position that is NOT a word boundary.

console.log("educate".match(/\\Bcat\\B/)); // ["cat"]

4. Special Sequences (Shorthand)

SequenceMeaningExample
\\dAny digit [0-9]/\\d/.test("5") → true
\\DAny non-digit/\\D/.test("a") → true
\\wAny word character [a-zA-Z0-9_]/\\w/.test("G") → true
\\WAny non-word character/\\W/.test("@") → true
\\sAny whitespace (space, tab, newline)/\\s/.test(" ") → true
\\SAny non-whitespace/\\S/.test("a") → true
.Any character except newline/./.test("x") → true

Practical Examples

// Dot matches any character
console.log(/a.c/.test("abc")); // true

// Start and end boundaries
console.log(/^Hello/.test("Hello World")); // true
console.log(/World$/.test("Hello World")); // true

// Optional character
console.log(/colou?r/.test("color")); // true
console.log(/colou?r/.test("colour")); // true

// Quantifiers
console.log(/a{2,4}/.test("aa")); // true
console.log(/a{2,4}/.test("aaaaa")); // false

// Lookahead
console.log(/\d(?=px)/.test("10px")); // true

// Word boundary
console.log(/\bcat\b/.test("The cat sat")); // true

// OR operator
console.log(/cat|dog/.test("dog")); // true

// Escaping
console.log(/a\.b/.test("a.b")); // true

 

Article 0 of 0