HTML and CSS Reference
In-Depth Information
EXAMPLE 17.14 ( CONTINUED )
<script type="text/javascript">
// Negation within a Character Class
1
var reg_expression = /[^0-9]/;
2
var textString=prompt("Type a string of text","");
3
var result=reg_expression.test(textString) ;// R eturns true
// or false
document.write(result+"<br />");
if (result){
document.write("<b>The reg_ex /[^0-9]/ matched the
string\""+ textString +"\".<br />");
}
else{
alert("No Match!");
}
</script>
</body>
</html>
EXPLANATION
1
The caret inside a character class, when it is the first character after the opening
bracket, creates a negation, meaning any character not in this range. This regular
expression matches a string that does not contain a number between 0 and 9.
2
User input is assigned to the variable textString . In this example, abc was entered.
3
The regular expression test() method will return true because the string abc does
not contain a character ranging from 0 to 9 (see Figure 17.15).
Figure 17.15 The user entered abc . It contains a character that is not in the range
between 0 and 9.
17.4.3 Metasymbols
Metasymbols offer an alternative way to represent a character class. For example, instead
of representing a number as [0-9] , it can be represented as \d , and the alternative for rep-
resenting a nonnumber [^0-9] is \D . Metasymbols are easier to use and to type than
metacharacters.
 
 
Search WWH ::




Custom Search