Regular expression are patterns to match in a string. A regular expression in javascript is an object type. This post gives an introduction on how to write a regular expression in javascript. Moreover, it gives quick reference using example on special characters of regular expression.

Check out Regular Expression Usage post to know more about regular expression usage.

Create Regulare Expression Object

You can use following 2 ways to create regular expression.

Method 1 : Using regular expression literal

/pattern/flags notation is used to create regular expression.

    
        //String
        var str = "Smooth Programming";
        
        // Regular Expression
        // We have used 2 flags. 
        // i in /ig is for ignore case while matching.
        // g in /ig is for perform global match
        var re1 = /smooth/ig;

        // Result
        var result = re1.test(str);
        console.log(result); // Returns true
    

There is a difference between regular expression literal and string. var re1 = /ab+/i is regular expression but var str = "/ab+/i" is a string.

Method 2: Call a constructor function of the RegExp object.

new RegExp(pattern[, flags]) is a constructor for creating regular expression objects.

    
        //String
        var str = "Smooth Programming";
        
        // Regular Expression
        // We have used 2 flags. 
        // i in second argument is for ignore case while matching.
        // g in second argument is for perform global match
        var re2 = new RegExp(/smooth/, "gi");

        // Result
        var result = re2.test(str);
        console.log(result); // Returns true
    

Flags in Regular Expression

FlagsDescription
gPerforms global match
iIgnore case during match
mPerform multiline match
uIndicates pattern is a sequence of unicode code points
yStart matching from the lastIndex propery of the regular expression object

Special Characters in Regular Expression

Special CharacterMeaning
Character Classes
.Finds any single character except line terminators. \n, \r etc…

Ex: “sky color is blue”.match(/.s/g) returns [“is”]
\dMatches a digit character.

Ex: “I like number 7”.match(/\d/g) returns [“7”]
\DMatches any character that is not a digit.

Ex: “I like number 7”.match(/\D/g) returns [“I”, ” “, “l”, “i”, “k”, “e”, ” “, “n”, “u”, “m”, “b”, “e”, “r”, ” “]
\wMatches any alphanumeric character including underscore.

Ex: “_ has 5.5$”.match(/\w/g) returns [“_”, “h”, “a”, “s”, “5”, “5”].
\WMatches any character which is not alphanumeric including underscore

Ex: “_ has 5.5$”.match(/\W/g) returns [” “, ” “, “.”, “$”]
\sMatches a single white space character, including space, tab, form feed, line feed, and other Unicode spaces.

Ex: “Smooth Programming”.match(/\w*\s/g) returns [“Smooth “]
\SMatches a single character other than white space.

Ex: “Smooth Programming”.match(/\w*\S/g) returns [“Smooth”, “Programming”]
\tMatches horizontal tab
\rMatches carriage return
\nMatches a line feed
\vMatches a vertical tab
\fMatches a form feed
[\b]Matches a backspace
\0Matches a Null character
\This is an escape character.

Ex: “space “.match(/s/g); matches s and not space. returns [“s”]
“space “.match(/\s/g); matches space. returns [” “]
Character Sets
[xyz]
[a-d]
Character sets matches any one of the enclosed characters.

Ex: “Smooth”.match(/[m-o]/g) or “Smooth”.match(/[mno]/g) returns [“m”, “o”, “o”]
[^xyz]
[^x-z]
Matches anything that is not in the enclosed characters

Ex: “Smooth”.match(/[^m-o]/g) or “Smooth”.match(/[^mno]/g) returns [“S”, “t”, “h”]
Alternation
x|yMatches either x or y.

Ex: “Smooth Programming is easy”.match(/Smooth|easy/g) returns [“Smooth”, “easy”]
Boundaries
^Matches begining of an input. If multiline flag is set to true, it also matches immediately after line break as well.

Ex: “An apple”.match(/^a/g); returns null but
“An apple”.match(/^A/g); returns [“A”]
$Matches end of an input. If multiline flag is set to true, it also matches immediately before line break as well.

Ex: “An apple”.match(/a$/g); returns null but
“An apple”.match(/e$/g); returns [“e”]
\bMatches begining or end of an input

Ex: “An apple”.match(/An\b/g); returns [“An”]
“An apple”.match(/pp\b/g); returns null
\BMatches not at the begining or end of an input

Ex: “An apple”.match(/An\B/g); returns null
“An apple”.match(/pp\B/g); returns [“pp”]
Quantifiers
x*Matches preceding item x 0 or more times

Ex: “smooth”.match(/o*/g) matches
[“”, “”, “oo”, “”, “”, “”]
x+Matches preceding item x 1 or more times

Ex: “smooth”.match(/o+/g) matches [“oo”]
x?Matches preceding item x 0 or 1 times

Ex: “smooth”.match(/o?/g) returns [“”, “”, “o”, “o”, “”, “”, “”]
x{n}Here, n is a positive number. Matches exactly n occurences of the preceding item x

Ex: “smooth”.match(/o{2}/g) returns [“oo”]
x{n,}Here, n is a positive number. Matches at least n occurences of the preceding item x

Ex: “smooth”.match(/o{2}/g) returns [“oo”]
x{n,m}Here, n and m are positive integers. Matches at least n and at most m occurences of the preceding item x.

Ex: “smooth”.match(/o{1,3}/g) returns [“oo”]
Assertions
x(?=y)Matches x only if x is followed by y.

Ex: “smooth programming is easy”.match(/smooth(?= programming)/g) returns [“smooth”]
x(?!y)Matches x only if x is not followed by y.

Ex: “smooth programming is easy”.match(/smooth(?!easy)/g) returns [“smooth”]

References

Regular Expression Documentation in Mozilla Foundation