HTML and CSS Reference
Figure 17.34 The user entered Dan Robbins as one of the alternatives. Sam Robbins or Tom
Robbins would also be okay.
Remembering or Capturing. Besides grouping, when the regular expression pattern
is enclosed in parentheses, the subpattern created is being captured, meaning the subpat-
tern is saved in special numbered class properties, starting with $1 , then $2 , and so on. For
example, in the grouping example where we created a regular expression: /(ma)/, capturing
will assign “ma” to $1 if “ma” if matched. We can say that “ma” is remembered in $1. If we
have the expression /(John) (Doe)/, “John” will be captured in $1 and “Doe” in $2 if the
pattern “John Doe” is matched. For each subpattern in the expression, the number of the
property will be incremented by one: $1, $2, $3, and so on. The dollar sign properties can
be applied to the RegExp object, not an instance of the object. and then used later in the
program as shown in Example 17.33.They will persist until another successful pattern
match occurs, at which time they will all be cleared. Even if the intention was to control the
greedy metacharacter or the behavior of alternation as shown in the previous grouping
examples, the subpatterns are automatically captured and saved as a side effect. 2 For more
information on this go to http://developer.netscape.com/docs/manuals/communicator/
textString = "Everyone likes William Rogers and his friends."
var reg_expression = /(William)\s(Rogers)/;
document.write(myArray); // Three element array
document.write( "<br>"+ RegExp.$1 + " "+RegExp.$2 +"<br>" );
/* alert(myArray + " "+ myArray);
match and exec create an array consisting of the string, and
the captured patterns. myArray is "William Rogers"
myArray is "William"
myArray is "Rogers".*/
2. It is possible to prevent a subpattern from being saved.