All TalkersCode Topics

Follow TalkersCode On Social Media - A Social Media Network for developers Join Now ➔

JavaScript Regex Match Example

Last Updated : Mar 11, 2024

JavaScript Regex Match Example

In this tutorial we will show you the solution of JavaScript regex match example, here we will show example for regex match. Regex means regular expressions and in our example we are going to show how to use match on regular expression.

We defined one string and using match function we match one word with that string if this word present in string it returns true and result word or it returns false as empty result.

Step By Step Guide On JavaScript Regex Match Example :-

Here we defined one string variable ‘str’ with some string and we defined another variable ‘rex’ using that we provided one word ‘we’ for checks that word present in string ‘str’ value.

Then for match with string we needs to append string variable ‘str’ with ‘match()’ method.

Here we passed parameter variable ‘rex’ so it will checks that word presented in string or not and result printed on console using console.log() method.

<!DOCTYPE html>
        const str="we are working under pressure and without pressure we can’t handle or learn anything properly";
        const rex=/we/gi;
        let res=str.match(rex);
  1. <!DOCTYPE html> tag which is instruct the web browser about what version of HTML file written in and it’s not have any ending tag.
  2. The<html> tag is used to indicate the beginning of HTML document.
  3. As above shown <head> tag is contains information about webpage and external file links are declared here. <title> tag is used for set the webpage title.
  4. Both <head> and <title> tags having their pair end tag, so we need to close the ending tags respectively. If you’re not closed anyone of ending tag properly that is also affect the webpage result.
  5. <body> tag is beginning of main coding part because it contain coding of entire website blocks and elements described here.
  6. In <script> tag we defined variable ‘str’ using ‘const’ keyword with string ‘we are working under pressure and without pressure we can’t handle or learn anything properly’ and variable ‘rex’ defined it contain word needs to find in string ‘str’ so we sets it value to ‘we’.
  7. In variable ‘rex’ we provided word as ‘/we/gi’, here that double slashes we need to provide between the word and ‘gi’ means g-global flag, i-case-insensitive search flag.
  8. The variable ‘res’ used to stores result of found word ‘rex’ in string ‘str’ then printed by console.log() method.
  9. Both </body>,</html> tags closed respectively. </body> tag indicates the end of body, Then </html> tag indicates the end of HTML document.

Conclusion :-

In conclusion we are able to know how to match regular expression in javascript.

When we execute our program on browser we can’t see the result so we have to use shortcut ‘ctrl+shift+j’ then right hand side console panel will open with array result of word ‘we’ occurs in string ‘str’ in our string we had two times presented word ‘we’ so it contain two times word ‘we’ in array, you can also try some other string with multiple times appeared any particular word.

Console.log() is a function in javascript which is used to print any kind of variables defined before in it or to just print any message that needs to be displayed to the user.

I hope this tutorial on JavaScript regex match example helps you and the steps and method mentioned above are easy to follow and implement.

Author Image About Anjali

Experienced Computer Programmer with a broad range of experience in technology. Strengths in application development and Object Oriented architecture design, front end programming, usability and multimedia technology. Expert in coding languages such as C, C++ Java, JavaScript, PHP and more.

Follow Anjali On Linkedin 🡪