Table of Contents
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 |
0 Comments