var s = "perschon.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
var m = s.match(/m./g); alert(m[0]); //me alert(m[1]); //mo
If
var s = "perschon.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 = "perschon.com"; var pat= new RegExp("me","g"); if (str.match(pat)) { alert("m"); }