Why javascript having .test(), .exec(), .match(), .search() and .replace()

Yeah!. Here is some of javascript pre-defined function which will help to search something in string and replace with string.

Javascript having more pre-defined functions with that. But most of us (sorry, I don’t know about you ¯_(ツ)_/¯ ) don’t use all of that functions. In most of the suituation we will create a new function with the concept of what the pre-defined function have.


var str = "This Is TechniqZone.com";

.test() method returns the boolean.

Check the given string is present or not, It will return boolean.


var regex = new RegExp("is");
var regex1 = /is/;
console.log(regex.test(str)); // true
console.log(regex1.test(str)); // true

Check the given string is present or not in globally, It will return boolean.


var regex = new RegExp("is", "g");
var regex1 = /is/g;
console.log(regex.test(str)); // true
console.log(regex1.test(str)); // true

Check the given string present or not(with case-insensitive)


var regex = new RegExp("zone","i");
var regex1 = /is/gi;
console.log(regex.test(str)); // true
console.log(regex1.test(str)); // true

.exec() Returns a result array, or null


var regex = new RegExp("is");
var regex1 = /is/;
console.log(regex.exec(str)); //["is", index: 2, input: "This Is TechniqZone.com"]
console.log(regex1.exec(str)); //["is", index: 2, input: "This Is TechniqZone.com"]

var regex = new RegExp("is","gi");
var regex1 = /is/gi;
console.log(regex.exec(str)); //["is", index: 2, input: "This Is TechniqZone.com"]
console.log(regex.exec(str)); //["Is", index: 5, input: "This Is TechniqZone.com"]
console.log(regex.exec(str)); //null
console.log(regex.exec(str)); //["is", index: 2, input: "This Is TechniqZone.com"]

when we use multiple .exec() for the same regex it will loop to the next present of the givent string and atlast it will return null. then continue the looping from first present.

Note : this loop is possible when regex contain global tag ‘/g’. otherwise it will return same present–>


console.log(regex1.exec(str)); //["is", index: 2, input: "This Is TechniqZone.com"]
console.log(regex1.exec(str)); //["is", index: 5, input: "This Is TechniqZone.com"]
console.log(regex1.exec(str)); //null
console.log(regex1.exec(str)); //["is", index: 2, input: "This Is TechniqZone.com"]

.match() Return a result array, or null


var regex1 = /is/; // == new RegExp("is");
var regex2 = /is/g; // == new RegExp("is", "g");
var regex3 = /is/gi; // == new RegExp("is", "gi");
console.log(str.match(regex1)); //["is", index: 2, input: "This Is TechniqZone.com"]
console.log(str.match(regex2)); //["is"]
console.log(str.match(regex3)); //["is", "Is"]

.search() Returns the index of the present


var regex1 = /is/; // == new RegExp("is");
console.log(str.search(regex1)); // 2

.replace() is replace the matched string


var regex = /is/;
str.replace(regex, str =&gt; `<span>${str}</span>`); &lt;!-- Th<span>is</span> Is TechniqZone.com --&gt; 

var regex = /is/gi;
str.replace(regex, str =&gt; `<span>${str}</span>`); &lt;!-- Th<span>is</span> <span>Is</span> TechniqZone.com --&gt; 

Leave a Reply

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