What is RegEx? and What is RegEx used for?. We have answered these questions in our previous post. If you want to read that post then go to this link What is RegEx?


RegEx (Regular Expression) Functions In JavaScript - 1


RegEx in JavaScript

Regex is used in almost all programming languages. Its logic and methods of working are the same. But the method of writing its syntax may be different. In this post, we will learn how to use RegEx in JavaScript.


First let's talk about a simple RegEx syntax:


let regex = /codexradar/; code-box


The syntax written above is a literal of RegEx in JavaScript.


console.log(regex); code-box

Now when we console.log this syntax, we get this output:


Output : /codexradar/


console.log(regex.source); code-box


If we console.log RegEx.source then we get this output:

Output : codexradar

We get simple text without forward slash.


Functions to Match Expressions

RegEx provides us functions that we use to match a regular expression to a given string. These functions are as follows:


1. exec() Function

When we match a regular expression to a string through this function, if it matches, it returns an array or else it returns null.


We can understand this by following example:


let regex = /codexradar/;

let string = "This is codexradar website.";

let result = regex.exec(string);

console.log(result); code-box


In this example above, Our regular expression matches with the string and we get an array.


If the codexradar word appears twice in the given string, then what will be the output?


let regex = /codexradar/;

let string = "This is codexradar website. The url is codexradar.com.";

let result = regex.exec(string);

console.log(result); code-box


Now if we see the output of this code given above, then we get only the first array of codexradar word. If you want that you get both arrays of codexradar word then we have to use g flag. g means global. That is, now the regular expression will find the codexradar word in the entire string and then its array will be given to us.


We can see this in the following code:


let regex = /codexradar/g; // g flag for global

let string = "This is codexradar website. The url is codexradar.com.";

let result = regex.exec(string);

console.log(result);

console.log(result);

console.log(result); code-box

Now if we see the output of the above example, in the first two results, we get both codexradar word arrays and in the third result we get null. Because there is no third codexradar word present in the string.


Similarly, we also have an i flag. Which checks the case insensitivity of Word. For example, you can see the example given below:  


let regex = /CodexRadar/i; // i flag for case insensitive

let string = "This is codexradar website. The url is CodexRadar.com.";

let result = regex.exec(string);

console.log(result); code-box


See the output of the code given above, we get only the array of first codexradar word. If we remove the i flag and run the code again then we will get an array of second CodexRadar word.

In the exec () function, we get an object as a result. The index of the word that is matched and the string's input is found in this object. We can see this in the following example:


console.log(result.index);

console.log(result.input); code-box


We get the index number and string of the word codexradar as output.


2. test() Function

When we test a regular expression through this function, we get true and false as output. Returns true when regular expression matches string, or else returns false.


let regex = /CodexRadar/;

let string = "This is codexradar website. The url is CodexRadar.com.";

let result = regex.test(string);

console.log(result); code-box


Output : true


3. match() Function

This function works just like the test() function. The only difference is that when the match is found then return array instead of true and when the match is not found then return null instead of false.


Its syntax is also different from the test() function. In the test() function, we test a regular expression on a string, its syntax is this regex.test(string) and in match() function we match the string with the regular expression, its syntax is this string.match(regex).


let regex = /CodexRadar/;

let string = "This is codexradar website. The url is CodexRadar.com.";

//let result = regex.match(string); //This syntax is Wrong

let result = string.match(regex); //This syntax is Right

console.log(result); code-box


Output : get array of CodexRadar word.


4. search() Function

If we want to do some search in the given string, we use this function.


let regex = /CodexRadar/;

let string = "This is codexradar website. The url is CodexRadar.com.";

let result = string.search(regex); 

console.log(result); code-box


Output : Return index of first match else return -1.


5. replace() Function

If we want to replace a specific word to a new word  in the given string, we use this function.


let regex = /codexradar/;

let string = "This is codexradar website. The url is codexradar.com.";

let result = string.replace(regex, 'CodexRadar'); 

console.log(result); code-box


Output : This is CodexRadar website. The url is codexradar.com.

This function returns a new replaced string in the output.


If the global flag is not used then only the first match will be replaced. alert-info


If we want to replace all the words in the given string with a new word, then we have to use the g flag in the regular expression.


let regex = /codexradar/;

let string = "This is codexradar website. The url is codexradar.com.";

let result = string.replace(regex, 'CodexRadar'); 

console.log(result); code-box


Output : This is CodexRadar website. The url is CodexRadar.com.


Summary

In this post, we learned about the functions of regular expressions in JavaScript. Now you practice this and in the next post we will learn about the metacharacters of regular expression. Keep visiting the blog to read more new similar posts.

Post a Comment

Previous Post Next Post