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
var m = s.match(/m./g); alert(m[0]); //me alert(m[1]); //mo
If
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)) { alert("m"); }