Regular Expressions: Character Classes


You also can put a number of characters into a character class . Such a class will match any single character in it. To create a character class, you enclose a number of characters in square brackets. We've already seen this example, in which we're searching for any vowel:

 <HTML>      <HEAD>          <TITLE>Checking for Vowels</TITLE>          <SCRIPT LANGUAGE="JavaScript">              <!--             function checkVowels()              {  var regexp = /[aeiou]/   var matches = regexp.exec(document.form1.text1.value)   if (matches) {   document.form1.text2.value = "Yes, we got vowels!"   } else {   document.form1.text2.value = "Sorry, no vowels!"   }  }              //-->          </SCRIPT>      </HEAD>      <BODY>          <H1>Checking for Vowels</H1>          <FORM NAME="form1">              Type some text:              <BR>              <INPUT TYPE="TEXT" NAME="text1">              <BR>              <INPUT TYPE="BUTTON" VALUE="Check for Vowels" ONCLICK="checkVowels()">              <BR>              <INPUT TYPE="text" NAME="text2" SIZE="30">          </FORM>      </BODY>  </HTML> 

You also can specify a range of characters in a character class, using a hyphen (-), as in this case, where I'm matching any upper case letter: [A-Z] . (If you actually want to match a hyphen, you should escape it as \-] .)

In addition, you can use ^ as the first character in a character classif you do, that character stands for "not" (and not the beginning of a line, which is what ^ matches outside a character class), meaning that the character class matches anything not in the character class. Here's an example that we saw at the beginning of the chapter that uses that technique, and which we can now understand (the \b stands for "word boundary," which matches the boundary between whitespace and a word character, or the boundary between a word character and following whitespace):

 <HTML>      <HEAD>          <TITLE>Getting Lower Case Words</TITLE>          <SCRIPT LANGUAGE="JavaScript">              <!--             function getLowers()              {  var regexp = /\b[^A-Z]+\b/   var matches = regexp.exec(document.form1.text1.value)   if (matches) {   for (var loopIndex = 0; loopIndex < matches.length; loopIndex++){   document.form1.text2.value += matches[loopIndex] + " "   }   } else {   document.form1.text2.value = "Sorry, no lower case words."   }  }              //-->          </SCRIPT>      </HEAD>      <BODY>          <H1>Getting Lower Case Words</H1>          <FORM NAME="form1">              <INPUT TYPE="TEXT" NAME="text1" VALUE="JavaScript is the subject." SIZE="30">              <BR>              <INPUT TYPE="BUTTON" VALUE="Get Lower Case Words" ONCLICK="getLowers()">              <BR>              <INPUT TYPE="text" NAME="text2" SIZE="30">          </FORM>      </BODY>  </HTML> 


Inside Javascript
Inside JavaScript
ISBN: 0735712859
EAN: 2147483647
Year: 2005
Pages: 492
Authors: Steve Holzner

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net