email address

RegExp:

apg-exp:

email address:

All phrases captured by all rules are retained along with the index to the first letter where the capture was found. Display of the character rules, local-char, sub-domain-char, top-domain-char, alpha, num & special has been suppressed for clarity. Note that the first rule in the syntax always defines the full phrase to be matched. result[0] is simply an alias for compatibility with RegExp.

Note: First-time users are often unclear about how, exactly, that big, multi-line ABNF pattern syntax is introduced into the apg-exp object. The short answer is, exactly the same way that a regular expression is introduced into the RegExp object. It is passed into the constructor as a single string.

var exp = new ApgExp(pattern);
var result = exp.exec(str);
The only difference between this and constructing a RegExp object is that the pattern syntax is a multi-line string. The ABNF named rules must be on separate lines and end with a newline (continuation lines begin with a space or tab).
  • In a web application, the pattern is often read from a textarea, as is done in this example (see the function executeApgExp()).
  • In a desktop setting, the pattern is often read from a multi-line text file.
  • For simple patterns or quick testing they might simply be introduced as constructed strings:
    var pattern = "";
    pattern += 'email-address   = %^ local "@" domain %$\n';
    pattern += 'local           = local-word *("." local-word)\n';
      ...
    

This syntax is taken from the Regular Expressions Cookbook by Goyvaerts and Levithan, pg. 245. Some groupings have been added to capture the local and domain parts ( result[1] & result[4], respectively) and the local, sub-domain and top-domain words. In the case where a single group captures more than one phrase, only the last capture is retained. This can be seen in this example with the "local word" and "sub domain" groups, result[3] & result[5], respectively.

Note that many other languages offer more features and even with RegExp there are good tools for easier use, more features and debugging help. But with native JavaScript RegExp, this is about all you get.