JS Regular Expression Tutorial | Character Sets



If you want to match a range of characters you can enclose it in []. As an example [pqrs] will match any of the characters 'p', 'q','r', 's'. We may also abbreviate a range of character, e.g., [p-s] will match any of the characters between 'p' to 's'. Let us see the following example.

[aeiou] matches john but not hymn
[5-9] matches 987, but not 2304
[a-z] matches john but not JOHN
[A-Z] matches JOHN but not john


We must also note that the ranges must be in ascending order. A pattern [9-0] is invalid and will throw an error.

We will now write a complete example.


<html>
<body>
<script type="text/javascript">
<!--
/*
********************************************************
Javascript Regular Expression Example 10
********************************************************
*/
var pattern1=new RegExp("^[A-Z][a-z][a-z]$");
var string1 = new Array(4);
string1[0] = "Lee";
string1[1] = "lee";
string1[2] = "You";
string1[3] = "Xa2";
var i;
for(i=0; i<=3; i++)
{
if (pattern1.test(string1[i]))
{
document.write(string1[i], " -> match found with ^[A-Z][a-z][a-z]$","<br\>");
}
else
{
document.write(string1[i], " -> match not found with ^[A-Z][a-z][a-z]$","<br\>");
}
}
//-->
</script>
</body>
</html>

Try this Example online


You can try this example online at - here .

The regular expression ^[A-Z][a-z][a-z]$ will capture any three character letter. The first character must be in Capital, and the next two must be small letters.

This code will produce following output.

Lee -> match found with ^[A-Z][a-z][a-z]$
lee -> match not found with ^[A-Z][a-z][a-z]$
You -> match found with ^[A-Z][a-z][a-z]$
Xa2 -> match not found with ^[A-Z][a-z][a-z]$
We can also combine the [ ] with { } to form a defined number of characters or literals.

Some examples


[0-9]{3} means a three letter digit , for example 234 or 567 or 709 but not A23 [a-z]{1,5} matches mike but not Michael

As a practical example consider we want to match a 1-800 number. A 1-800 number is a toll free number in US (for those who do not live in US) and has pattern like 1-800- 123-5648. Here is the pattern for that

1-800-[0-9]{3}-[0-9]{4}

Notice that we have combines the character set with the curly bracket to perform powerful regular expression.

We can also coming two ranges in a single [ ] bracket. For example [0-9a-z] will match any number digit between 0 to 9 or any character between a to z

Let us write the complete example which will find if a given telephone number is a valid 1-800 number.


<html>
<body>
<script type="text/javascript">
<!--
/*
********************************************************
Javascript Regular Expression Example 11
Valid 1-800 Number
********************************************************
*/
var pattern1=new RegExp("^1-800-[0-9]{3}-[0-9]{4}$");
var string1 = new Array(4);
string1[0] = "1-800-235-2345";
string1[1] = "1-866-235-2345";
string1[2] = "18002352345";
string1[3] = "1-800-2352345";
var i;
for(i=0; i<=3; i++)
{
if (pattern1.test(string1[i]))
{
document.write(string1[i], " -> match found with ^1-800-[0-9]{3}-[0-9]{4}$","<br\>");
}
else
{
document.write(string1[i], " -> match not found with ^1-800-[0-9]{3}-[0-9]{4}$","<br\>");
}
}
//-->
</script>
</body>
</html>

Try this Example online


You can try this example online at - here .

The code gives the following output.

1-800-235-2345 -> match found with ^1-800-[0-9]{3}-[0-9]{4}$
1-866-235-2345 -> match not found with ^1-800-[0-9]{3}-[0-9]{4}$
18002352345 -> match not found with ^1-800-[0-9]{3}-[0-9]{4}$
1-800-2352345 -> match not found with ^1-800-[0-9]{3}-[0-9]{4}$


We may also like to make the - sign optional. The pattern for making the - optional can be ^1-?800-?[0-9]{3}-?[0-9]{4}$. A complete example for this is given below

<html>
<body>
<script type="text/javascript">
<!--
/*
********************************************************
Javascript Regular Expression Example9c
Valid 1-800 Number
********************************************************
*/
var pattern1=new RegExp("^1-?800-?[0-9]{3}-?[0-9]{4}$");
var string1 = new Array(4);
string1[0] = "1-800-235-2345";
string1[1] = "1-866-235-2345";
string1[2] = "18002352345";
string1[3] = "1-800-2352345";
var i;
for(i=0; i<=3; i++)
{
if (pattern1.test(string1[i]))
{
document.write(string1[i], " -> match found with ^1-?800-?[0-9]{3}-?[0-9]{4}$","<br\>");
}
else
{
document.write(string1[i], " -> match not found with ^1-?800-?[0-9]{3}-?[0-9]{4}$","<br\>");
}
}
//-->
</script>
</body>
</html>

Try this Example online


You can try this example online at - here .

Time for another short quiz. Click next.