Regex Metacharacters: Understanding Key Symbols
Explore the key metacharacters used in regular expressions, including their functions for matching patterns. Learn about symbols like '.', '^', and '$' and their roles in defining text patterns.
Metacharacters in Regex
In regular expressions, metacharacters are special characters that define patterns for matching a wide range of text combinations. Here is a list of commonly used metacharacters:
Metacharacter | Description |
---|---|
. | Any single character |
^ | Match the beginning of a line |
$ | Match the end of a line |
a|b | Match either a or b |
\d | Any digit |
\D | Any non-digit character |
\w | Any word character |
\W | Any non-word character |
\s | Any whitespace character |
\S | Any non-whitespace character |
\b | Word boundary |
\B | Not a word boundary |
[\b] | Backspace character |
\xYY | Match hex character YY |
\ddd | Octal character ddd |
Examples of Metacharacters
The period .
metacharacter matches any single character. For example, the pattern /./g
matches every character in the string.
Example
var regex = /./g;
var str = "Hello World!";
var matches = str.match(regex);
console.log(matches); // Output: ['H', 'e', 'l', 'l', 'o', ' ', 'W', 'o', 'r', 'l', 'd', '!']
The ^
and $
are anchors that match the start and end of a line, respectively.
Start and End of String
var startRegex = /^He/g;
var endRegex = /ld!$/g;
var str = "Hello World!";
var startMatch = str.match(startRegex);
var endMatch = str.match(endRegex);
console.log(startMatch); // Output: ['He']
console.log(endMatch); // Output: ['ld!']
The |
character represents "or" in regex. For instance, /e|o/g
matches either e
or o
in the text.
Example
var regex = /e|o/g;
var str = "Hello World!";
var matches = str.match(regex);
console.log(matches); // Output: ['e', 'o', 'o']
The \b
metacharacter represents a word boundary. For example, the pattern /\b\w+\b/g
finds every word in a string.
Example
var regex = /\b\w+\b/g;
var str = "Hello World! 123";
var matches = str.match(regex);
console.log(matches); // Output: ['Hello', 'World', '123']
The \d
finds any digit, while \D
finds any non-digit character. The \w
searches for any alphanumeric character and underscore, and \W
works opposite of \w
.
Digits and Non-Digits
var digitRegex = /\d/g;
var nonDigitRegex = /\D/g;
var wordRegex = /\w/g;
var nonWordRegex = /\W/g;
var str = "A12B34C567 *890X";
var digits = str.match(digitRegex);
var nonDigits = str.match(nonDigitRegex);
var words = str.match(wordRegex);
var nonWords = str.match(nonWordRegex);
console.log(digits); // Output: ['1', '2', 'B', '3', '4', 'C', '5', '6', '7', '8', '9', '0']
console.log(nonDigits); // Output: ['A', 'B', 'C', ' ', '*', 'X']
console.log(words); // Output: ['A', '1', '2', 'B', '3', '4', 'C', '5', '6', '7', '8', '9', '0', 'X']
console.log(nonWords); // Output: [' ', '*']
Multiple metacharacters can be combined to create more complex patterns. For example, /\s./g
finds the first character after a space.
Example
var regex = /\s./g;
var str = "Hello World! How are you?";
var matches = str.match(regex);
console.log(matches); // Output: ['o ', 'W ', 'H ', 'a ', 'y ']