JavaScript Regular Expression (RegEx)

JavaScript Regular Expression (RegEx)

A regular expression is a collection of characters that forms a search pattern. This search pattern can be used to perform text search, text replace and text validation operations, as shown below:

Operation Description Example
search() We can do a case sensitive search by using an identifier i. In this case we don’t have to search the exact data. var str = “Search Me!”;

var n = str.search(“/me/i”);

Output: 7

replace() We can replace a string using a case insensitive expression. var str = “Replace Me!”;

var res = str.replace(/me/i, “Yourself”);

Output: Replace Yourself!

test() We can test if a given string has a presence of required character or not. var email = “myemail@gmail.com”;

var res = (/@/.test(email));

Output: true

Author

  • Barry Allen

    A Full Stack Developer with 10+ years of experience in different domain including SAP, Blockchain, AI and Web Development.

    View all posts

Comments

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.