Author
Metacharacters are special symbols in JavaScript regular expressions that control pattern matching behavior. They allow you to match patterns beyond literal characters.
\\n).s flag, it also matches newlines.console.log("cat".match(/c.t/)); // ["cat"]
console.log("c\\nt".match(/c.t/s)); // ["c\\nt"]
m) mode, matches the start of each line.console.log(/^hello/.test("hello world")); // true
console.log(/^world/m.test("hello\\nworld")); // true
console.log(/world$/.test("hello world")); // true
console.log(/hello$/m.test("hello\\nworld")); // true
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"]
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"]
console.log("cat".match(/ca?t/)); // ["cat"]
console.log("ct".match(/ca?t/)); // ["ct"]
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"]
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"
Defines a character class, matching any single character inside the brackets.
console.log("cat".match(/[a-c]/g)); // ["c", "a"]
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 timesconsole.log("caaat".match(/ca{2,4}t/)); // ["caaat"]
\\d, \\w, etc.console.log("a.b".match(/a\\.b/)); // ["a.b"]
console.log("123".match(/\\d+/)); // ["123"]
Matches the position between a word character (\\w) and a non-word character (\\W).
console.log("hello world".match(/\\bworld\\b/)); // ["world"]
Matches a position that is NOT a word boundary.
console.log("educate".match(/\\Bcat\\B/)); // ["cat"]
| Sequence | Meaning | Example |
|---|---|---|
\\d | Any digit [0-9] | /\\d/.test("5") → true |
\\D | Any non-digit | /\\D/.test("a") → true |
\\w | Any word character [a-zA-Z0-9_] | /\\w/.test("G") → true |
\\W | Any non-word character | /\\W/.test("@") → true |
\\s | Any whitespace (space, tab, newline) | /\\s/.test(" ") → true |
\\S | Any non-whitespace | /\\S/.test("a") → true |
. | Any character except newline | /./.test("x") → true |
// 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