HTML and CSS Reference
TABLE 3-1 Regular expression special characters
The caret character denotes the beginning of a string.
The dollar sign denotes the end of a string.
The period indicates to match on any character.
Alphabet letters indicate to match any alphabetic character. This is case-sensitive. To match
lowercase letters, use [a-z].
This combination indicates to match any numeric character.
The plus sign denotes that the preceding character or character set must match at least
The asterisk denotes that the preceding character or character set might or might not match.
This generates zero or more matches.
When included in a character set, the caret denotes a negation. [^a] would match a string
that doesn't have an 'a' in it.
The question mark denotes that the preceding character is optional.
This combination indicates to match a word character consisting of any alphanumeric
character, including an underscore.
The backslash is an escape character. If any special character should be included in the
character set to match on literally, it needs to be escaped with a \. For example, to find a
backslash in a string, the pattern would include \\.
This combination indicates to match on a space. When it's combined with + or *, it can
match on one or more spaces.
This list encompasses the main functions available when string matching with regular
expressions. Building regular expressions requires taking the definition of those characters
and essentially creating a mask out of them to be used by the regular expression engine
to interpret and decide whether there is a match. For example, a Canadian postal code is
comprised of the format A1A 1A1—that is, alternating alphabetic characters and numeric
characters with a space in the middle. Some characters aren't used in postal codes because
the machines confuse them with other characters (for example, Z and 2). Also, the space isn't
mandatory. When you need to enforce the data format of the user input, deciding how you
want the data to be captured and how flexible you want it to be is important. Then build your
regular expression to match this.
Now, build the regular expression for a postal code. You first need to denote the begin-
ning of the string, because it helps eliminate unnecessary white space at the lead of the string:
The first part of the expression is the caret. The next character must be alphabetic: