Negated Character Class

Suppose you want to search for a pattern that does NOT contain any digit. This is done by using [^0-9]. The caret literal ^ placed just after the left bracket implies that whatever follows is negated. Note that the caret sign ^ is also used to denote the beginning of the string.

Here are some examples of the negated character class.

^[^a-zA-Z]{2}$ Matches 23 but not me
^[^a-z]{1}[a-z]{2}$ Matches You but not you.

Note that the first caret sign ^ in ^[^a-zA-Z]{2}$ denotes that the string must start with the pattern following it.

Here is a complete example.


<html>
<body>
<script type="text/javascript">
<!--
/*
********************************************************
Javascript Regular Expression Example14
Understanding use of Negated Character Class
********************************************************
*/
var pattern1=RegExp("^[^a]{4}$");
var string1 = new Array(4);
string1[0] = "yahu";
string1[1] = "your";
string1[2] = "mine";
string1[3] = "admission";
var i;
for(i=0; i<=3; i++)
{
if (pattern1.test(string1[i]))
{
document.write(string1[i], " matches ^[^a]{4}$","<br\>");
}
else
{
document.write(string1[i], " does not match ^[^a]{4}$ ", "<br\>");
}
}
//-->
</script>
</body>
</html>

Try this Example online


You can try this example online at - here .

This script will give the following output

yahu does not match ^[^a]{4}$
your matches ^[^a]{4}$
mine matches ^[^a]{4}$
admission does not match ^[^a]{4}$


Here are some more examples of the negated character class.

[^a-zA-Z] Matches 23 but not michael
[^A-Z] Matches michael and michael23 but not MICHAEL
[^A-Z0-9] Matches michael but not MICHAEL23
[^0-9] Matches Michael but not 23


Let us write another example. I will change my style of writing here. You have noticed that we have a common header and a common footer in all examples so far. The common header in all examples was

<html>
<body>
<script type="text/javascript">
<!â??


The common footer in all examples was

//-->
</script>
</body>
</html>


In all examples henceforth we will not write these headers and footers required to make a complete javascript code. You may like to include them in your code to make it complete. This saves some pages and keeps our environment green. So here is the code that will find if a word is formed only with vowels.

/*
********************************************************
Javascript Regular Expression Example 15
Find if a word is formed only with vowels
********************************************************
*/
var pattern1=RegExp("[^aeiou]");
var string1 = new Array(4);
string1[0] = "eau";
string1[1] = "vowel";
string1[2] = "oui";
string1[3] = "javascript";
var i;
for(i=0; i<=3; i++)
{
if (pattern1.test(string1[i]))
{
document.write(string1[i], " -> has some consonants","<br\>");
}
else
{
document.write(string1[i], " -> all vowels","<br\>");
}
}


Try this Example online


You can try this example online at - here .

If you run this code you get the following output

eau -> all vowels
vowel -> has some consonants
oui -> all vowels
javascript -> has some consonants