Things you need to know about regular expression in JS (Part I)

Regular expressions are patterns used to match character combinations in strings. Regular expressions are a powerful way of doing search and replace in strings.
To define regular expressions,you can place your pattern within two forward slashes or can use RegExp object as shown below
Syntax:-

Some useful regular expression methods:
1st:exec()
This returns an array of information if patterns matches given sring or null otherwise:


To get information of next occurance use flag ‘g’ (gloabl) along with the expression as shown here →


few more useful flags,

syntax to use flag:var regex = /pattern/flags;
var re = new RegExp('pattern', 'flags');
2nd test(): Returns true if pattern is present in string otherwise returns false

3rd match():Return an array containing all of the matches, or null if no match is found. Here is an example:

as we are using ‘g‘ flag, match() method return array of all matching elements as:

4th search():Returns the index of the match pattern or -1 otherwise.

5th replace(): Replaces the matched substring with a replacement substring.

consoles
