Predefined Character Classes
"inline"; } function showCode(displayCodePage, codePath) { var codePathEls = codePath.split("/"); var currDocPathEls = location.href.split("/"); //alert ("codePathEls = " + codePathEls + "\n" + "currDocPathEls = " + currDocPathEls); currDocPathEls.pop(); // remove file name at the end while (codePathEls.length > 0) { if (codePathEls[0] == "..") { codePathEls.shift(); currDocPathEls.pop(); } else { break; } } var fullCodePath = currDocPathEls.join("/") + "/" + codePathEls.join("/"); //alert ("fullCodePath = " + fullCodePath ); if iv>
Trail: Essential Classes
Lesson: Regular Expressions


Trail: Essential Classes
Lesson: Regular Expressions

Predefined Character Classes

The Pattern API contains a number of useful predefined character classes, which offer convenient shorthands for commonly used regular expressions:

Construct Description
. Any character (may or may not match line terminators)
\d A digit: [0-9]
\D A non-digit: [^0-9]
\s A whitespace character: [ \t\n\x0B\f\r]
\S A non-whitespace character: [^\s]
\w A word character: [a-zA-Z_0-9]
\W A non-word character: [^\w]

In the table above, each construct in the left-hand column is shorthand for the character class in the right-hand column. For example, \d means a range of digits (0-9), and \w means a word character (any lowercase letter, any uppercase letter, the underscore character, or any digit). Use the predefined classes whenever possible. They make yoa capital letter means the opposite:

  • \D matches non-digits
  • \S matches non-spaces
  • \W matches non-word characters

Previous page: Character Classes
Next page: Quantifiers