HTML and CSS Reference
EXAMPLE 17.14 ( CONTINUED )
// Negation within a Character Class
var reg_expression = /[^0-9]/;
var textString=prompt("Type a string of text","");
var result=reg_expression.test(textString) ;// R eturns true
// or false
document.write("<b>The reg_ex /[^0-9]/ matched the
string\""+ textString +"\".<br />");
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.
User input is assigned to the variable textString . In this example, abc was entered.
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.
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