More Character Classes



Besides [..] and [^..], there are some more commonly used character classes. For example \s is used to match space character, tab character or any other Unicode whitespace character. The table below lists these character classes and their brief explanation.

\d Match any digit in the range 0 to 9. Equivalent to [0-9]
\D Match any character not in the range 0-9. Equivalent to [^0-9]
\w Match any word character in the range 0-9,a-z,A-Z_. Equivalent to [0-9a-zA-Z_]. Notice the underscore.
\W Match any character NOT in the range 0-9, a-z,A- Z_. Equivalent to [^0-9a-zA-Z_]
\s Whitespace character.
\S Any Character that is not a white space character.
. Any character except new line. Equivalent to [^\x0A\x0D\u2028\u2029]
\b matches a word boundary (the position between a word and a space)
\B Match every position that is not a word boundary


We will try to understand the salient points of these character classes with some examples.

/*
********************************************************
Javascript Regular Expression Example16
Understanding use of whitespace character \s
********************************************************
*/
var pattern1=/\s/;
var string1 = new Array(3);
string1[0] = "Michael Jackson";
string1[1] = "michael";
string1[2] = "Michael Jackson"; // a tab
var i;
for(i=0; i<=2; i++)
{
if (pattern1.test(string1[i]))
{
document.write(string1[i], " -> has whitespace character","<br\>");
}
else
{
document.write(string1[i], " -> does not have whitespace charcter","<br\>");




Try this Example online


You can try this example online at - here .

This script gives the following output

Michael Jackson -> has whitespace character
michael -> does not have whitespace charcter
Michael Jackson -> has whitespace character
Here is another example for usage of \w


/*
********************************************************
Javascript Regular Expression Example17
Understanding use of word character \w
********************************************************
*/
var pattern1=/\w/;
var string1 = new Array(3);
string1[0] = "*&$#";
string1[1] = "michaeljacson";
string1[2] = "*&_$#";
var i;
for(i=0; i<=2; i++)
{
if (pattern1.test(string1[i]))
{
document.write(string1[i], " -> has word character","<br\>");
}
else
{
document.write(string1[i], " -> does not have word character","<br\>");
}
}


Try this Example online


You can try this example online at - here .

This gives out following output

*&$# -> does not have word character
michaeljacson -> has word character
*&_$# -> has word character


Here is another example that explains the use of the . (dot) symbol .

********************************************************
Javascript Regular Expression Example18
/b.t/ matches "bat", "bit", "bet" and so on.
********************************************************
*/
var pattern1=/^b.t$/;
var string1 = new Array(3);
string1[0] = "bat";
string1[1] = "bit";
string1[2] = "b6t";
string1[3] = "b*t";
var i;
for(i=0; i<=3; i++)
{
if (pattern1.test(string1[i]))
{
document.write(string1[i], " -> matches /^b.t$/","<br\>");
}
else
{
document.write(string1[i], " -> does not matches /^b.t$/","<br\>");
}
}



Try this Example online


You can try this example online at - here .

It gives the following output

bat -> matches /^b.t$/
bit -> matches /^b.t$/
b6t -> matches /^b.t$/
b*t -> matches /^b.t$/