Open In App

Underscore.js _.isRegExp() Function

Improve
Improve
Like Article
Like
Save
Share
Report

_.isRegExp() function:

  • It finds whether the object passed is a regular expression or not.
  • If the object is a regular expression then it returns true otherwise false.
  • We can even apply operations like addition etc on the variables in which the result of _.isRegExp() is stored.

Syntax:

_.isRegExp(object)

Parameters: It takes only one argument which is the object that needs to be checked.

Return value: It returns true if the object passed is a regular expression and if not then false is returned.

Examples:

  1. Passing a regular expression to the _.isRegExp() function: The _.isRegExp() function takes the element from it’s parameter and starts checking if it is a regular expression or not. Since the object starts and ends with ‘/’, therefore it is a regular expression. Hence, the result is true.




    <html>
        
    <head>
        <script src
        </script>
    </head>
        
    <body>
        <script type="text/javascript">
            console.log(_.isRegExp(/geek/));
        </script>
    </body>
        
    </html>

    
    

    Output:

  2. Passing a string to the -.isRegExp() function: In this we are passing a string to the _.isRegExp() and this can be identified as the parameter passed is inside the ‘ ‘ (quotes). Since a string is not a regular expression therefore, the output will be false.




    <html>
        
    <head>
        <script src
        </script>
    </head>
        
    <body>
        <script type="text/javascript">
            console.log(_.isRegExp('geek'));
        </script>
    </body>
        
    </html>

    
    

    Output:

  3. Passing a string with ‘/’ to _.isRegExp() function: The _.isRegExp() function takes the parameter which in this case is inside ‘ ‘, hence it is a string. Therefore, all the letters, symbols inside ‘ ‘ will behave as a string character. Hence the overall object is a string. Therefore, the output is false.




    <html>
        
    <head>
        <script src
        </script>
    </head>
        
    <body>
        <script type="text/javascript">
            console.log(_.isRegExp('/geek/'));
        </script>
    </body>
        
    </html>

    
    

    Output:

  4. Applying addition operation on the _.isRegExp() function’s output:
    In this we are storing the result of both the example 1 and 2 in the variables ‘a’ and ‘b’. Then we are applying addition operation on both the ‘a’ and ‘b’ variables. Since ‘a’ is true and ‘b’ is false, therefore, the addition of true and false will result in 1 which is then stored in ‘c’ variable.




    <html>
        
    <head>
        <script src
        </script>
    </head>
        
    <body>
        <script type="text/javascript">
            var a=_.isRegExp(/geek/);
            var b=_.isRegExp('geek');
            var c=a+b;
            console.log(a, b, c);
        </script>
    </body>
        
    </html>

    
    

    Output:

NOTE: These commands will not work in Google console or in firefox as for these additional files need to be added which they didn’t have added. So, add the given links to your HTML file and then run them. The links are as follows:




<!-- Write HTML code here -->
<script type="text/javascript" src =
</script>




Last Updated : 25 Nov, 2021
Like Article
Save Article
Previous
Next
Share your thoughts in the comments
Similar Reads