HTML and CSS Reference
In-Depth Information
EXAMPLE
17.19 (
CONTINUED
)
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 variable is assigned a regular expression containing an uppercase letter,
[A-
Z]
, followed by zero or more lowercase letters,
[a-z]*
, and a space,
\s
. There are
either zero or more lowercase letters.
2
The variable
textString
is assigned user input; in this example,
Danny boy
was en-
tered.
3
The regular expression test method will return
true
because the string
Danny boy
matches an uppercase letter
D
, followed by zero or more lowercase letters
anny
,
and a space. See Figure 17.21.
Figure 17.21
The user entered
Danny boy
, consisting of one uppercase letter, zero
or more lowercase letters, and a space (top); the user entered
DANNY BOY
,
consisting one uppercase letter, zero lowercase letters, and a space (bottom).
Note: the “Y” is followed by a space.