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:

OperationDescriptionExample
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

Leave a comment

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