HTML and CSS Reference
In-Depth Information
be represented as
[abde]
. The pattern
/ab|de/
is either
ab
or
de
, whereas the class
[abcd]
represents only
one
character in the set
a
,
b
,
c
, or
d
.
EXAMPLE
17.31
<html>
<head><title>Alternation</title></head>
<body>
<script type="text/javascript">
//
Alternation: this or that or whatever...
1
var reg_expression = /Steve|Dan|Tom/;
var textString=prompt("Type a string of text","");
2
var result=reg_expression.test(textString)
;//
Returns true
//
or false
document.write(result+"<br />");
if (result){
document.write("<b>The regular expression /Steve|Dan|Tom/
matched the string\""+ textString +"\".<br />");
}
else{
alert("No Match!");
}
</script>
</body>
</html>
EXPLANATION
1
The pipe symbol, |, is used in the regular expression to match on a set of alterna-
tive patterns. If any of the patterns,
Steve
,
Dan
, or
Tom
, are found, the match is
successful.
2
The
test()
method will return
true
if the user enters either
Steve
,
Dan
, or
Tom
. See
Figure 17.33.
Figure 17.33
The user entered
Do you know Tommy?
. Pattern
Tom
was matched in the string.