Characters in Regex

In the regex pattern, a character is anything from a single letter of the alphabet to a numeric digit that you want to search for. This is the most straightforward component of the regex pattern.

Include the word(s) or a single character in the pattern you are searching for in the text.

The following example checks whether a string contains the specified character or not:

/ /g

In the above example, the regex pattern contains a single character "H" which needs to be searched in the input string "Hello World!". Since the input string contains "H", the regex will match "H".

The following searches a sequence of multiple characters:

/ /g
/ /g
/ /g

Note that regex is case-sensitive by default in most programming languages.

/ /g

Search for Special Characters

Regex supports the following special characters that can be included as characters:

  • newline '\n'
  • carriage return '\r'
  • tab '\t'
  • Whitespace '\s'
  • Null character '\0'
  • Form feed '\f'
  • ASCII character \nnn

The following searches for the whitespace in the string:

/ /g

Escape Characters

Some characters have a special meaning in regex, such as . $ ^ { [ ( | ) * + ? \. Other than these characters, regular expressions have no special meaning; they match themselves. You will learn about these characters in the next few chapters.

However, sometimes you would like to include one or more special characters without special meaning in regex. For example, you may want to search for $ in the text but do not want to consider $ as a special character. For that, you must use the backslash \  as an escape character.

For example, the following finds $ character in the input string:

/ /g

The following table lists the result of regex search of different patterns on different input strings:

Regex Pattern Input String Match
"He" "Hello World!" "Hello World!"
"he" "Hello World!" ""
"Helo" "Hello World!" ""
"Wor" "Hello World!" "Hello World!"
"orld" "Hello World!" "Hello World!"
"\tW" "Hello World" "Hello World"
"abd" "abcd efg hi" ""
"b" "abcd efg hi" "abcd efg hi"
"cde" "abcd efg hi" ""
"cd\se" "abcd efg hi" "abcd efg hi"
"1" "A123-4B31" "A123-4B31"
"-4" "A123-4B31" "A123-4B31"

Learn about other special characters called metacharacters next.