Regular expressions are used to find patterns in a string. Below are the common uses for regular expression in javascript. If you are new to regular expression, then check out introduction to regular expression in javascript post before continuing on this post.

Following are the most common usage of regular expression in javascript.

Use 1 : RegExp.exec() to find first matching string

The regexObj.exec(str) method look for a first match in a specified string and returns result array of matching string. If no matching string found then returns null. If you want to get all matching string then use String.match() method or run exec() method in a for loop with “g” flag.


        var str = "Smooth Programming is a Full Stack Developers blog. Smooth Programming is for smooth learning.";
        var re1 = /smooth (programming)*/ig;
        var result = re1.exec(str);
        console.log(result);
        // ["Smooth Programming", "Programming", index: 0, input: "Smooth Programming is a Full Stack Developers blog. Smooth Programming is for smooth learning."]
        // result[0] is full string of characters matched
        // result[1...n] are parenthesized substring matches
        // result.index is the index of matched string
        // result.input is the input string
    

Use 2 : String.match() to find all matching strings

The str.match(regexp) method get all matching strings against regular expression. It returns an array containing all matching strings.

regexObj.exec(str) returns first match found and str.match(regexp) returns all matching string found. Regular Expression without ‘g’ flag in match function returns the same result as RegExp.exec() method.


        var str = "Smooth Programming is a Full Stack Developers blog. Smooth Programming is for smooth learning.";
        var re1 = /smooth (programming)*/ig;
        var result = str.match(re1);
        console.log(result);
        // ["Smooth Programming", "Smooth Programming", "smooth "]


        // Regular Expression without 'g' flag in match function returns the same result as RegExp.exec() method
        // ------------------------------------------------------------------------------------------------------
        var re2 = /smooth (programming)*/i;
        var result2 = str.match(re2);
        console.log(result2);
        // ["Smooth Programming", "Programming", index: 0, input: "Smooth Programming is a Full Stack Developers blog. Smooth Programming is for smooth learning."]
        // result[0] is full string of characters matched
        // result[1...n] are parenthesized substring matches
        // result.index is the index of matched string
        // result.input is the input string
    

Use 3 : RegExp.test() to check pattern exists in string or not

The regexObj.test(str) look for a match between regular expression and input string. Returns true if match found, else returns false.


        var str = "Smooth Programming!";
        var result = /smooth/i.test(str);
        console.log(result);  // true
    

Use 4 : String.search() to get index of matching pattern in string

The str.search(regexp) method executes a search for a match between regular expression and input string. Returns index of the first match found else returns -1.

str.search(regexp) returns index of the first match and regexObj.test(str) returns boolean indicating match exists in a string or not.


        var str = "Smooth Programming";
        var re1 = /programming/ig;
        var result = str.search(re1);
        console.log(result); // 7
    

Use 5 : String.replace() to replace some or all matching pattern with new string

str.replace(regexp|substr, newSubStr|function) method returns a new string with some or all matches replaced with new string.

Regular Expression with ‘g’ flag will replace all matching string else it will replace only first matching string. If substr is used as a first argument then it will only replace only first matching string.


        var str = "Smooth Programming is for Smooth Learning";

        // Regular expression without g flag will replace only first match
        var result1 = str.replace(/smooth/i, "Easy");
        console.log(result1); // Easy Programming is for Smooth Learning

        // Regular expression with g flag will replace all match
        var result2 = str.replace(/smooth/ig, "Easy");
        console.log(result2); // Easy Programming is for Easy Learning

        // substring will replace only first match
        var result3 = str.replace("Smooth", "Easy");
        console.log(result3); // Easy Programming is for Smooth Learning
    

Use 6 : String.split() to split string with specified separator

str.split([separator[, limit]]) method split input string into array of substring as per the matching separator. You can use regular expression as a separator as shown in below example.


    var months = "Jan, Feb, Mar; Apr, May; Jun, Jul, Aug, Sept; Oct, Nov, Dec";
    
    // Split part of the string by ',' or ';'
    var result = months.split(/,\s|;\s/ig);
    console.log(result);
    // ["Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug", "Sept", "Oct", "Nov", "Dec"]

    // Limit number of split to 4.
    var result2 = months.split(/,\s|;\s/ig, 4);
    console.log(result2);
    // ["Jan", "Feb", "Mar", "Apr"]