JavaScript tutorial-special characters in regular expression

Regular Expression

Special characters

To write more complex string patterns, you need to know how to use special characters in the string patterns. The special characters and examples are listed below:

Special Character Description
\dMatches any digit character from 0 to 9. Example: /\d\d\d/ matches 123 but not 12a ore a12,...
\DMatches any character that is not a digit. Example: /\D/ matches A but not 1.
\sMatches any character that is a whitespace including tab, carriage return, form feed, new line, and vertical tab. Example: /\s/ matches tab.
\SMatches any character that is not a whitespace character. Example: /\S/ matches A but not tab.
\wMatches any character that is a character from a to z, A to Z, and underscore. Example: /\w/ match _ but not 0.
\WMatches any character that is a not a character from a to z, A to Z, and underscore. Example: /\W/ matches @ but not a.
[...]Matches any character that is in  brackets. Example: /[ab]/ matches a or b only.
[^...]Matches any character that is not in bracket. Example:/ [^ab]/ matches A or B but not a or b.
.Matches any character that is not a new line. Example /./ matches a,0, or %
?Matches the previous item 0 time or 1 time. Example: /J?/ matches J or not at all.
*Matches the previous item 0 time or more times. Example: /J*/ matches J, JJ, JJJ, or not at all
+Matches the previous item 1 time or more times. Example: /J+/ matches J,JJ, or JJJ.
{n}Matches the previous item n times. Example:/\d{2}/ matches 34, 44, or 89.
{n,}Matches the previous item more than n times. Example: /\d{1,}/ matches 0, 10,287,3897, or 90000.
{n,m}Matches the previous item from n to m times. Example: /\d{1,3}/ matches 1, 23, or 897.
^Matches a pattern at the beginning of a string.
$Matches a pattern at the end of a string.
\bMatches a word boundary position.
\BMatches a position that is not a word boundary.

For example, now you want a user to enter his/her phone number in a text box in the following example format: (855)89-256-724. To check the phone number validity(conforming to this format), your pattern should be:

var stpat=/[(]855[)]\d{2}-\d{3}-\d{3}/gi;

To help you understand the pattern, we break it in to small parts:

[(] and [)] match opening and closing parentheses.

/855/ matches 855.

/\d{2}/ matches any two digits.

/-/ matches the minus sign.

/\d{3}/ matches any three digits.

Now let see this task is done on the browser by executing the code:

<title>Text field element</title>
<script language="JavaScript" type="text/javascript">
function check(){
var ph=document.forms[0].phone.value;
var stpat=/[(]855[)]\d{2}-\d{3}-\d{3}/gi;
if(!=-1) alert("Valid!");
else alert("Invalid!");
<form action="checkphone.html">
Enter phone number: <input type="text" name="phone" /><br />
<input type="button" onclick="check()" value="Submit">

Grouping Expressions

You can group more than one expressions together. For example, you have a string a like "Java Programming, JavaScript Programming are the same?". How can you write a pattern to match Java Programming and JavaScript Programming? This kind of problem can be solved by grouping Java and JavaScript patterns together in parentheses. Java and JavaScript both end with Programming, so the pattern should be:

var stpat=/\b(Java)?\b(JavaScript)\bProgramming\b/;

Reusing groups of characters

For example, you have a string like "JavaScript,HTML,HTML,CSS,PHP,ASP, CSS,CSS,CSS,CSS". In fact this string should contain only distinct words. How would you mark the duplicate words and replace them with "Removed"? You can solve this problem by providing group number(\1 is refers to group 1, \2 refers to group 2 and so on):

var stpat=/([a-z]+), \1/gi;

<title>Regular Expression-reusing groups of characters</title>

<script language="JavaScript" type="text/javascript">

var st= "JavaScript,HTML,HTML,JavaScript,CSS,PHP,ASP,CSS,CSS,CSS,CSS";
var stpat= /([a-z]+),\1/gi;
var stnew=st.replace(stpat,"Removed");



This website intents to provide free and high quality tutorials, examples, exercises and solutions, questions and answers of programming and scripting languages:
C, C++, C#, Java, VB.NET, Python, VBA,PHP & Mysql, SQL, JSP, ASP.NET,HTML, CSS, JQuery, JavaScript and other applications such as MS Excel, MS Access, and MS Word. However, we don't guarantee all things of the web are accurate. If you find any error, please report it then we will take actions to correct it as soon as possible.