Saturday, December 28, 2024
Google search engine
HomeLanguagesJavascriptUnderscore.js _.isRegExp() Function

Underscore.js _.isRegExp() Function

_.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>


Whether you’re preparing for your first job interview or aiming to upskill in this ever-evolving tech landscape, neveropen Courses are your key to success. We provide top-quality content at affordable prices, all geared towards accelerating your growth in a time-bound manner. Join the millions we’ve already empowered, and we’re here to do the same for you. Don’t miss out – check it out now!

RELATED ARTICLES

Most Popular

Recent Comments