Metacharacters in Regex
In regular expressions, a metacharacter is a special character that has a specific meaning using which you can build complex patterns that can match a wide range of combinations.
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 | matches any whitespace character |
\S | Match any non-whitespace character |
\b | Matches a word boundary |
\B | Match must not occur on a \b boundary. |
[\b] | Backspace character |
\xYY | Match hex character YY |
\ddd | Octal character ddd |
Let's see the most commonly used metacharacter in the regex pattern and see the result.
The period .
metacharacter matches with any single character. The pattern /./g returns the string itself because it matches every character, as shown below.
The ^ and $ are also referred as anchors. The ^ searches from the start of a string.
The $ matches the end of string.
The | char represent or in regex. So, `e|o` pattern searches either 'e' or 'o' character in the text.
The \b represents the word boundary. The following find every word in the string.
The \d finds any digit.
The following \D searches for any non-digit character.
The \w searches for any alphanumeric character and underscore e.g. character class [A-Za-z0-9_]. The \W works opposite of \w.
The following table lists regex patterns and matches on different input text considering the global g flag:
Input String | Pattern | Description | Total matches | Result |
---|---|---|---|---|
Hello World! | . | Match any character | 12 | 'H', 'e', 'l', l'', 'o', ' ', 'W', 'o', 'r', 'l', 'd', '!' |
Hello World! | ..o | Match 'a' that follows any two characters | 4 | 'llo', ' Wo' |
Hello World! | ..l | Match 'l' that follow any two characters | 2 | 'Hel', 'orl' |
Hello World! | ..l.. | Match 'l' that is between any two characters | 2 | Hello, orld! |
Hello World 123 | \S | Find any non-space character | 13 | 'H', 'e', 'l', l'', 'o', 'W', 'o', 'r', 'l', 'd', '1', '2', '3' |
Hello World 123 | \s | Find a space character | ' ', ' ' (Two white spaces) | |
Hello World! | ^H | Find 'H' that starts from the line | 1 | 'H' |
Hello World! | ^Hello | Find 'Hello' that starts from the line | 1 | 'Hello' |
Hello World! | ^hello | Find 'hello' that comes at the starting of the string | 0 | No match found |
Hello World! | ^World | Find 'World' that starts from the line | 0 | No match found |
Hello World! | World!$ | Find 'World' that comes at end of string. | 1 | 'World!' |
Hello World! | Worl$ | Find 'Worl' that comes at end of string. | 0 | No match found |
Hello World! | ...$ | Find any three characters from the end of the string | 1 | 'ld!' |
Hello World! | ..\s | Find any two characters before space in the string | 1 | 'lo ' |
Multiple metacharacters can also be used in combination with other characters.
The following pattern finds the first character after a space using two metacharacters \s
and .
.