| Refresh | Home EGTry.com

extract all instances of matched pattern


retrieve all occurrences of matched substring of a pattern. One match occurrence may contains several groups.

commentsJavascript sourceExecution result in your current browser
"g" in RegExp("\\w+","g") is for match all occurrences
var text="john.smith@gmail.com\nuser.name@domain.com";
var pattern=new RegExp("\\w+","g");
var result;
var i=0;
while ( (result=pattern.exec(text))  !=null ) {
  document.write(result[0]+"  ("+result.index+","+pattern.lastIndex+")<br/>");
  i++;
  if (i>10) break;
}