HTML and CSS Reference
In-Depth Information
Matching a Substring
The most basic regular expression consists of a string of characters specified as follows
/ chars /
where chars is the text of the substring. For example, the regular expression
/Audio/
matches any text string containing the sequence of characters Audio . Thus, the text string
Audio Studio would be matched by this regular expression; however, because regular
expressions are case sensitive, the text string audio studio would not be matched.
Setting Regular Expression Modifiers
To change a regular expression so that it matches patterns regardless of case, you add
a modifier to the regular expression. The modifier to make the expression insensitive to
case (so that upper- and lower-case letters are treated the same) is the i character, added
to the regular expression as follows:
/ pattern /i
Thus, the regular expression
/Audio/i
would match the text string audio studio . By default, a regular expression returns
only the first occurrence of a specified pattern. To allow a global search for all pattern
matches, you append the regular expression with the character g as follows:
/ pattern/ g
Finally, to apply both modifiers at the same time, you append both the i and g flags to
the regular expression:
/ pattern /ig
Therefore the expression
/Audio/ig
would return two matches from the text Audio Studio for audiophiles .
Defining Character Positions
The examples you've seen so far are very basic because they involve matching only spec-
ified characters found anywhere within a text string. The true power—and complexity—
of regular expressions comes with the introduction of special characters that allow you
to match text strings based on the type, content, and placement of those characters. The
first such characters you will consider are positioning characters. The four positioning
characters are described in Figure H-1.
Search WWH ::




Custom Search