HTML and CSS Reference
In-Depth Information
EXAMPLE 17.21
<html>
<head><title></title></head>
<body>
<script type="text/javascript">
1
var reg_expression = /abc\d{1,3}\.\d/;
2
var textString=prompt("Type a string of text","");
3
var result=reg_expression.test(textString); // Returns true
// or false
document.write(result+"<br />");
if (result){
document.write("<b>The regular expression
/abc\\d{1,3}\\.\\d/ matched the string\""+
textString +"\".<br />");
}
else{
alert("No Match!");
}
</script>
</body>
</html>
EXPLANATION
1
The variable is assigned a regular expression containing the pattern abc\d{1,3}\.\d ,
where abc is followed by at least one digit, repeated by up to three digits, followed
by a literal period, and another digit, \d .
2
The variable textString is assigned user input; here, abc456.5xyz was entered.
3
The regular expression contains the curly brace {} metacharacters, representing
the number of times the preceding expression will be repeated. The expression
reads: Find at least one occurrence of the pattern \d and as many as three in a row.
See Figure 17.23.
Figure 17.23 The user entered abc followed by between one and three numbers,
followed by a literal period, and xyz (top); the entered string matched true
(bottom).
Search WWH ::




Custom Search