HTML and CSS Reference
In-Depth Information
EXAMPLE 17.23
<html>
<head><title></title></head>
<body>
<script type="text/javascript">
//Repeating patterns
1
var reg_expression = /5{1,}\.\d/;
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 #\\5{1,}\\.\\d/
matched the string\" " + textString + "\".<br />");
}
else{
alert("No Match!");
}
</script>
</body>
</html>
EXPLANATION
1
The variable, called reg_expression , is assigned a regular expression. The regular
expression reads: Search for the number 5, followed by at least 1 or more of them
(the curly braces determine how many consecutive 5s to look for), followed by a
literal period and a digit.
2
The test() method returns true if the regular expression pattern was found in the
input string. See Figure 17.25.
Figure 17.25 The user entered abc5555555.2 , or the number 5 at least 1 time,
followed by a literal period, and any digit, \d (top). This returns true ; the user
entered 5.6 (bottom). This also returns true .
Metacharacters That Turn off Greediness. By placing a question mark after a
greedy quantifier, the greed is turned off and the search ends after the first match, rather
than the last one.
Search WWH ::




Custom Search