Click here to Skip to main content
15,848,451 members
Articles / Web Development / HTML

Simple and Useful JavaScript Regular Expression Tutorial

Rate me:
Please Sign up or sign in to vote.
4.36/5 (17 votes)
16 Jul 2012CPOL2 min read 48.8K   407   23   9
This post discusses some simple and useful JavaScript Regular Expressions


Regular Expression is a very important part of any language programming because of its ability to help programmers to write fast and pure applications.

You Don't Know What a Regular Expression Is?

A regular expression is an object that describes a pattern of characters.

Regular expressions are used to perform pattern-matching and "search-and-replace" functions on text. 

Web developer can also use Regular Expression in JavaScript. Now I describe some simple examples of using Regular Expression in JavaScript.

The overall syntax should look like:


  • pattern specifies the search pattern
  • modifiers specify if our search should be case-sensitive, global, etc.

Modifiers Table

iPerform case-insensitive matching
gPerform a global match (find all matches rather than stopping after the first match)
mPerform multiline matching

To compile and use Regular Expression, you can use three useful functions.

Regular Expression Object Methods

compile()Compiles a regular expression
exec()Tests for a match in a string. Returns the first match
test()Tests for a match in a string. Returns true or false

Character Escapes  (Metacharacters)

Metacharacter Description
Find a single character, except newline or line terminator 
\wFind a word character 
\W Find a non-word character 
\dFind a digit 
\DFind a non-digit character 
\sFind a whitespace character 
\SFind a non-whitespace character 
\bFind a match at the beginning/end of a word 
\BFind a match not at the beginning/end of a word 
\0 Find a NUL character
\nFind a new line character 
\f Find a form feed character 
\r Find a carriage return character 
\t Find a tab character 
\v Find a vertical tab character 
\xxxFind the character specified by an octal number xxx 
\xddFind the character specified by a hexadecimal number dd 
\uxxxxFind the Unicode character specified by a hexadecimal number xxxx 

Metacharacter table from W3Schools.  


Test sentence: Sometext sometext sometext

ExpressionTest Result
/sometext/Sometext sometext sometext
/sometext/iSometext sometext sometext
/sometext/gSometext sometext sometext
/sometext/giSometext sometext sometext
/some/Sometext sometext sometext
/some/iSometext sometext sometext
/some/gSometext sometext sometext
/some/igSometext sometext sometext

It's easy!

In the next article, I will show you Regular Expression performance in some cases and also how to make simple and pure Regular Expressions.


  • 19 May, 2011: Initial version.
  • 1 June, 2011: Added sample source code.
  • 16 July, 2012: Add metacharacters table.


This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

Written By
Iran (Islamic Republic of) Iran (Islamic Republic of)
I am a web developer, the co-founder of
I love programming and also new technologies on Web Developing such as HTML5 and I always split my time between programming and reading.

Comments and Discussions

GeneralMy vote of 5 Pin
Abdul Quader Mamun14-Aug-12 3:28
Abdul Quader Mamun14-Aug-12 3:28 
QuestionNot an article Pin
Ganesan Senthilvel16-Jul-12 3:48
Ganesan Senthilvel16-Jul-12 3:48 
QuestionRe: Not an article Pin
Afshin Mehrabani16-Jul-12 6:11
Afshin Mehrabani16-Jul-12 6:11 
GeneralMy vote of 1 Pin
Dave Kreskowiak16-Jul-12 2:46
mveDave Kreskowiak16-Jul-12 2:46 
GeneralMy vote of 1 Pin
davidlow7023-May-11 15:14
davidlow7023-May-11 15:14 
GeneralThanks Pin
Ahmad Hyari21-May-11 9:39
Ahmad Hyari21-May-11 9:39 
GeneralMy vote of 3 Pin
AspDotNetDev20-May-11 11:37
protectorAspDotNetDev20-May-11 11:37 
GeneralRe: My vote of 3 Pin
Afshin Mehrabani21-May-11 7:28
Afshin Mehrabani21-May-11 7:28 
GeneralRe: My vote of 3 Pin
davidlow7023-May-11 15:21
davidlow7023-May-11 15:21 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.