HTML and CSS Reference
In-Depth Information
17.4.2 The Character Class
A character class represents one character from a set of characters. For example [abc]
matches either an a , b , or c; and [a-z] matches one character from a set of characters in
the range from a to z ; and [0-9] matches one character in the range of digits between 0
to 9 . If the character class contains a leading caret, ^, then the class represents any one
character not in the set; thus, [^a-zA-Z] matches a single character not in the range from
a to z or A to Z , and [^0-9] matches a single digit not in the range between 0 and 9.
JavaScript provides additional symbols, called metasymbols, to represent a character
class. The symbols \d and \D represent a single digit and a single nondigit, respectively;
the same as [0-9] and [^0-9] ; whereas \w and \W represent a single word character and
a single nonword character, respectively; same as [A-Za-z_0-9] and [^A-Za-z_0-9] .
EXAMPLE 17.12
<html>
<head><title>The Character Class</title></head>
<body>
<script type="text/javascript">
1
var reg_expression = /[A-Z][a-z]eve/;
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 reg_ex /[A-Z][a-z]eve/ matched the
string\""+ textString +"\".<br />");
}
else{
alert("No Match!");
}
</script>
</body>
</html>
EXPLANATION
1
The variable is assigned a bracketed regular expression containing alphanumeric
characters. This regular expression matches a string that contains at least one up-
percase character ranging between A and Z, followed by one lowercase character
ranging between a and z, followed by eve .
2
The variable textString is assigned user input, in this example Steven lives in Cleve-
land was entered.
3
The regular expression test() method will return true because Steven contains an
uppercase character, followed by a lowercase character, and eve . Cleveland also
matches the pattern. The variable result contains either true or false . See the out-
put in Figures 17.12 and 17.13.
 
 
Search WWH ::




Custom Search