JavaScript RegExp Metacharacters
Special characters with special meanings in regular expressions
🔤 What are Metacharacters?
Metacharacters are special characters in regular expressions that have specific meanings. They help you create powerful search patterns instead of matching literal characters.
// Metacharacters have special meanings
let pattern = /\d+/; // \d means "any digit"
let text = "I have 5 cats";
console.log(text.match(pattern)); // ["5"]
Common Metacharacters
\d - Digits
Matches any digit (0-9)
\w - Word Characters
Matches letters, digits, underscore
\s - Whitespace
Matches spaces, tabs, newlines
. - Any Character
Matches any character except newline
🔹 Digit Metacharacters (\d and \D)
Use \d to match digits and \D to match non-digits:
let text = "Room 123, Floor 4";
// Match digits
let digits = text.match(/\d/g);
console.log(digits); // ["1", "2", "3", "4"]
// Match multiple digits together
let numbers = text.match(/\d+/g);
console.log(numbers); // ["123", "4"]
// Match non-digits
let nonDigits = text.match(/\D+/g);
console.log(nonDigits); // ["Room ", ", Floor "]
Output:
digits: ["1", "2", "3", "4"]
numbers: ["123", "4"]
nonDigits: ["Room ", ", Floor "]
🔹 Word Character Metacharacters (\w and \W)
\w matches letters, digits, and underscores. \W matches everything else:
let text = "[email protected]";
// Match word characters
let wordChars = text.match(/\w/g);
console.log(wordChars); // ["u","s","e","r","_","n","a","m","e","1","2","3","e","m","a","i","l","c","o","m"]
// Match word sequences
let words = text.match(/\w+/g);
console.log(words); // ["user_name123", "email", "com"]
// Match non-word characters
let nonWords = text.match(/\W+/g);
console.log(nonWords); // ["@", "."]
Output:
words: ["user_name123", "email", "com"]
nonWords: ["@", "."]
🔹 Whitespace Metacharacters (\s and \S)
\s matches spaces, tabs, and newlines. \S matches non-whitespace:
let text = "Hello\tWorld\n JavaScript";
// Match whitespace
let spaces = text.match(/\s/g);
console.log(spaces); // ["\t", "\n", " ", " "]
// Match non-whitespace sequences
let words = text.match(/\S+/g);
console.log(words); // ["Hello", "World", "JavaScript"]
// Replace multiple spaces with single space
let cleaned = text.replace(/\s+/g, " ");
console.log(cleaned); // "Hello World JavaScript"
Output:
words: ["Hello", "World", "JavaScript"]
cleaned: "Hello World JavaScript"
🔹 Dot Metacharacter (.)
The dot (.) matches any character except newline:
let text = "cat, bat, hat";
// Match 3-letter words ending in 'at'
let pattern = /.at/g;
let matches = text.match(pattern);
console.log(matches); // ["cat", "bat", "hat"]
// Match any character between 'c' and 't'
let pattern2 = /c.t/;
let result = "cut".match(pattern2);
console.log(result); // ["cut"]
// Escape dot to match literal period
let sentence = "End of sentence.";
let period = sentence.match(/\./);
console.log(period); // ["."]
Output:
matches: ["cat", "bat", "hat"]
result: ["cut"]
period: ["."]
🔹 Practical Examples
Common uses of metacharacters in real applications:
Phone Number Validation:
// Match phone format: (123) 456-7890
let phonePattern = /$$\d{3}$$ \d{3}-\d{4}/;
let phone = "(555) 123-4567";
console.log(phonePattern.test(phone)); // true
Extract Words from Text:
// Get all words from a sentence
let sentence = "Hello, world! How are you?";
let words = sentence.match(/\w+/g);
console.log(words); // ["Hello", "world", "How", "are", "you"]
Clean Extra Spaces:
// Remove extra whitespace
let messy = "Too many spaces";
let clean = messy.replace(/\s+/g, " ");
console.log(clean); // "Too many spaces"