[Javascript] Gnarly RegEx issue...

David T. Lovering dlovering at gazos.com
Wed Mar 26 09:40:03 CST 2003


I'm trying to find a way to do FAST in-line parsing of input strings from a field on a character by character basis.  I have a technique which works beautifully at present that uses a RegEx mask in combination with a bunch of prototypes of the correct
syntax, but in order for the method to work you must list ALL possible syntax prototypes in your initial array.  Needless to say, for exceedingly complicated syntax structures this would be nasty.

For example, if I wanted to do a syntax check of a field on a character-by-character basis (irrespective of Browser, which is why I don't use keyCode comparisons) for a 800/888 toll-free phone number, I'd do it like this:

  var object;
  function isTollFree(object) {
    if (!object) return false;
    if (object == '') return false;
    var thisEntry = object.value;
    if (thisEntry.length == 0) return false;
    var realChar = thisEntry.charAt(thisEntry.length - 1);	// get last character entered
    thisEntry.replace(/.$/, "");							// nuke last character 
    object.value = thisEntry;

    var mask = /^((800|888))\-[1-9][0-9]{2}\-[0-9]{4}$/;
    var fake = new Array("800-347-2902","888-524-6909");

    if (realChar == '0x08') { return true; }				// validate BS, DEL, and TAB keys without echo
    if (realChar == '0x09') { return true; }
    if (realChar == '0x10') { return true; }

    for (var i=0; i<fake.length; i++) {
      var dummy = thisEntry + realChar + fake[i].substring(thisEntry.length + 1, fake[i].length);
      if (dummy.search(mask) != -1) {
        if (fake[i].charAt(thisEntry.length + 1) == "-") {
          object.value += realChar + '-';
          return true;
        }
        object.value += realChar;
        return true;
      }
    }
    return false;
  }

  Since the character actually has to be in the field to be visible to this routine, I trigger the event on
'keyUp' instead of 'keyPress' or 'keyDown', which work on the falling edge of the key cycle.

  HOWEVER (!!!) for very complicated structures (like addresses), even though the mask is still manageable, the number of permutations which must be put in the 'fake' array approaches being NP complete (aka 'very huge').
Not only does some wonk have to work all these out and type them in initially, but the syntax checking loop can
run for tens of thousands of cycles before exhausting the possibilities.

  WHAT I WANT... is a way to use merely the mask itself to check an incomplete string without using this rather half-assed method of completing the input with every possible syntax 'fill' derived from a fake prototype.  As
noted earlier, I've explored vanilla uses of match, search, and exec, and they all seem to require a complete
match against the mask before they validate (hence my hokey work-around).  Is there anything else out there which will do a regex check only on the first [thisEntry.length + 1] characters?  Enquiring minds want to know.

-- Dave Lovering


More information about the Javascript mailing list