regular expressions – part ii 1. the exec method is more complicated than i realized, and something you will probably not use in this cours
Regular expressions – Part II
1. The exec method is more complicated than I realized, and something
you will probably not use in this course.
If pat is a regexp then pat.exec(target) examines target to see if it
has the expression described in pat and returns an array if there is a
match, or null otherwise.
If a=pat.exec(target) then a[0] is the matched text, a[1] is the text
which matches the first parenthesized subexpression in pat, a[2] is
the text which matches the second parenthesized subexpression in pat,
etc.
You may also step through all the results (matches) that exec() finds
– see handout from Flanagan’s JavaScript: the Definitive Guide.
2. pat.test(target) will return true (if there is a match) or false
(if there is no match).
3. The following characters must be escaped in regular expressions
defined with /s (but not when you define pat=new RegExp(“mystring”):
^ $ . * + ? = ! : | \ / ( ) [ ] { }
4. The following special characters are denoted with escape sequences:
\n newline (\u000A)
\t tab (\u0009)
\0 null (\u0000)
\v vertical feed
\f form feed
\r carriage return
\xnn any Latin character expressed in Hex as nn
\uxxxx any Unicode character expressed in Hex as xxxx
\cX the character ctl-X
[\b] a backspace
5. The following groups of characters are found in part I of Regular
Expressions:
\d digits \D non-digits
\s whitespace \S non white-space
\w alphanumeric, _ \w non-alphanumeric, non underscore
\b is a word boundary
Ex: From Danny Goodman’s JavaScript Bible
target=”Oh, hello, do you want to play Othello in the school play?”
var myRegExpression = /hello/;
In concert with some regular expression or string object methods, this
pattern matches the
string “hello” wherever that series of letters appears. The problem is
that this simple pattern
causes problems during the loop that searches and replaces the strings
in the example string:
It finds not only the standalone word “hello,” but also the “hello” in
“Othello
Fortunately, regular expressions provide a shortcut way to specify
general characteristics, including a feature known as a word boundary.
The symbol for a word boundary is \b (backslash, lowercase b). If you
redefine the pattern to include these specifications on both ends of
the text to match, the regular expression creation statement looks
like this:
var myRegExpression = /\bhello\b/;
When JavaScript uses this regular expression as a parameter in a
special string object
method that performs search-and-replace operations, it changes only
the standalone word
“hello” to “hi,” and passes over “Othello” entirely.
6. Exact matches: As you know from Part I, ^ requires that the match
be at the beginning of target, and $ requires that the match be at the
end. So
pat= /^Simmons$/
will match only when target is exactly the string Simmons.