JavaScript is useful for a lot more than opening pop-ups. If you use HTML forms on your website, and want to make sure that your visitors submit valid data on those forms, you might want to consider using some regular expressions in JavaScript. Alejandro Gervasio explains how, with many excellent examples.
Regular expressions in JavaScript - Character Escaping (Page 3 of 9 )
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.
Character Sets
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:
[aeiou]
which means any vowel.
Or something like this:
[12345]
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.
For example:
[^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.