| Refresh | Home EGTry.com

regular expression match and extract string parts


commentsJavascript sourceExecution result in your current browser
search - regex version of indexOf
 var line="first.last@egtry.com";
 var pos=line.search(/@\w+/);
 if (pos >-1) {
    document.write("matched username: "+line.substring(0,pos));
  } else {
    document.write("not matched");
  }

match - subgrouping
 var matches=line.match(/^([^@]+)@(.*)$/);
 if (matches ==null) {
   document.write("not matched");
 } else {
   document.write("matched part: "+matches[0]+"<br/>");
   document.write("username: "+matches[1]+"<br/>");
   document.write("domain: "+matches[2]+"<br/>");
  }