HTML and CSS Reference
In-Depth Information
EXAMPLE
17.20
<html>
<head><title></title></head>
<body>
<script type="text/javascript">
1
var reg_expression = /[A-Z][a-z]+\s/;
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 /[A-Z][a-z]+\\s/
matched the string\""+ textString +"\".<br />");
}
else{
alert("No Match!");
}
</script>
</body>
</html>
EXPLANATION
1
The regular expression reads: Search for an uppercase letter, followed by one or
more lowercase letters, followed by a space.
2
The user is prompted for input.
3
The regular expression
test()
method checks that the string
textString
entered by the
user matches the regular expression and returns
true
or
false
(see Figure 17.22).
Figure 17.22
The user entered
Danny Boy
or one uppercase letter, one or more
lowercase letters, and a space (top); the user entered
DannyBoy
and gets no
match, because there was not a space in the search string (bottom).