JS match

match() method checks whether a pattern exists or not in a string. If it matches, return the matched substring.

var s = "endmemo.com";
var m = s.match(/end/);
alert(m); //end
//If matches multiple times, return the first
var m = s.match(/m./);
alert(m); //me

If //g flag is used and there are multiple matches, return all matches as an array.

var m = s.match(/m./g);
alert(m[0]); //me
alert(m[1]); //mo

If () is used and no g flag, return the first match and all matches in () as an array.

var s = "endmemo.com";
var m = s.match(/m(.)/);
alert(m[0]); //me
alert(m[1]); //e
var m = s.match(/m(.)/g);
alert(m[0]); //me
alert(m[1]); //mo
alert(m[2]); //undefined

Use variables in string match

var str = "endmemo.com";
var pat= new RegExp("me","g");
if (str.match(pat))

endmemo.com © 2024  | Terms of Use | Privacy | Home