Regex with .test() In JavaScript
REGEX is very interesting and helping
RegEx |
It is an Acronym means Regular Expression
The Work of Regex
Regular Expressions (Regex) Regular Expression, or regex or regexp inshort, it is tremendously powerful in searching and manipulating text sub-strings of any String, particularly in processing text files. One line of regex can easily replace multiple lines of programming codes
Regex work as to search and return true if the search regex gets found
like in any string we have to search any particular words if that word of clause exist in that given string or other datatypes then it the .test() function will return True
This is the Alternative Code for Regex Expression and .test()
var str1= "Hello My Name Is Shivam Dixit";
var result=false;
var search="Shivam";
function regextest(str1,result,search){
for (var i=0;i<str1.length;i++){
if (str1[i]===search[0]){
for(var j=1;j<search.length;j++){
if (str1[i+j]===search[j]){
result = true; }
else{ result = false;
break; }
}
}
}return result;}
console.log(regextest(str1,result,search));
Here is Regex Function with Test Function
var str1= "Hello My Name Is Shivam Dixit";
var result=false;
var search=/Shivam&Dixit/;
var search1=/Shivam|test/;
var search2=/Shivam/;
result = search.test(str1);
result1 = search1.test(str1);
result2 = search2.test(str1);
console.log(result); // output true : always boolean output
console.log(result1); // false
console.log(result2); // true
That all about regex expression and .test()
Comments
Post a Comment