Sometimes, meta-meaning characters, such as (^) or ($) and other special ones need to be included within the string to be searched for, representing the corresponding character instead of having the special meaning in the context of regular expressions syntax. To do so, we need to escape them properly in the string, with a backslash. If a backslash has to be represented too, it must be escaped with another backslash (two slashes \\).
Lets see it in action:
\^ is used to mark the beginning of the string // Matches any string with a caret (^) in it.
\$ is used to mark the end of the string // Matches any string with the dollar sign ($) in it.
Anything enclosed in the special square brace brackets [ and ] is a character class, a set of characters to which a matched character must belong. Please note that the expression in the square brackets matches only a simple character.
We can list a set, such as:
which means any vowel.
Or something like this:
which matches 1 and 3 but not a or 6.
We can also describe a range, or set of ranges with the special hyphen character:
[1-5] // Same as previous example.
[a-z] // Matches any lowercase letter.
[a-zA-Z] // Matches any alphabetic character in lowercase or uppercase.
[0-9a-zA-Z] // Matches any letter or digit.
Besides, we can use sets to specify that a character cannot be a member of a set.
[^a-z] // Matches any character that is not between a and z.
The caret symbol means "not" when it is placed inside the square brackets. As we have seen previously, it has a different meaning when its used outside, anchoring the beginning of a string.