How To Use Regex In JavaScript
Last Updated : Mar 11, 2024
IN - JavaScript | Written & Updated By - Dikshita
In this tutorial we will show you the solution of how to use regex in JavaScript, here we will show example with regex and regex means regular expressions.
In our example we are going to show how to use methods of ‘search(), exec() and replace()’ on regular expression.
We defined one string and each functions definition with regular expression then printed each methods result on console using console.log() method.
Step By Step Guide On How To Use Regex In JavaScript :-
Here we defined one string variable ‘str’ with some string and we defined variables ‘rex1,rex2,rex3’ for storing each method results.
The search() method using to search a letter in string and it returns the index of first occurrence only, method exec() found word or letter we provide as result with index and provided string and method replace() used to replace a letter or word in provided string.
Replace() also replaces the first occurrence only in string ‘str’ then results printed on console using console.log() method.
<!DOCTYPE html> <html> <head> <title>REGULAR EXPRESSION USE</title> </head> <body> <script> const str="we are working under pressure and without pressure we can’t handle or learn anything properly"; let rex1=str.search(/e/i); let rex2=/u/.exec(str); let rex3=str.replace(/p/i,"y"); console.log(rex1); console.log(rex2); console.log(rex3); </script> </body> </html>
- <!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.
- The<html> tag is used to indicate the beginning of HTML document.
- 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.
- 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.
- <body> tag is beginning of main coding part because it contain coding of entire website blocks and elements described here.
- 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 ‘rex1’ defined it contain index of found letter ‘e’ in string ‘str’ so we gets result as ‘1’.
- Here ‘/e/i’ that double slashes we need to provide between the word or letter needs to find and i-case-insensitive search flag.
- variable ‘rex2’ defined it contain result of letter ‘u’, it’s index in string ‘str’ and string we provided.
- variable ‘rex3’ defined it contain result of replaced letter ‘p’ with letter ‘y’ in string ‘str’ and this method returns replaced string ‘str’.
- Each methods work on for first occurrence only in string str then results printed by console.log() method.
- 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 use regular expression with some methods 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 it contain three methods result.
Here we used console.log() method for display on console.
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 how to use regex in JavaScript helps you and the steps and method mentioned above are easy to follow and implement.