HTML and CSS Reference
In-Depth Information
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
jsguide/reobjud.hmt#1007373 .
<script type="text/javascript">
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[1] + " "+ myArray[2]);
match and exec create an array consisting of the string, and
the captured patterns. myArray[0] is "William Rogers"
myArray[1] is "William"
myArray[2] is "Rogers".*/
2. It is possible to prevent a subpattern from being saved.
Search WWH ::

Custom Search