CommuterJoy » Logbook

« logbook home

Posted by mattc at Jul 7, 08 06:18 PM ... Comments (0)

It would be nicer if RegExp objects assigned the mismatched portions of the test string to an array in a similar fashion to which they store portions of successful pattern matches.

 var foo = /^[a-z]+$/;
 foo.exec("34asdf"); // returns null

So, the developer knows the input string ('34asdf') is invalid but not why. It would much more useful to the calling method to know the specifics of why the string didn't match the pattern and, say, in the cases of interfaces and web forms be able to communicate with precision to the user the reason why their input has failed not just that it has.

Eg. Given the regex test above, it would be useful to be able to look up a failure property that has been populated as a result of the test,

 foo.failures = [ "substring '3' found at position 1, expected '[a-z]'", ... ];

I'm not aware of any language that implements regex's doing something like this. In JS you'd have to either throw away your regex pattern and write a lot of custom substring checking functions or split your regex in to multiple parts, but by doing this you lose some of their concise beauty.

Comments (0)

Post Your Comments

random bookmark
link summary month October 2009 (1)
September 2009 (14)
August 2009 (16)
July 2009 (21)
June 2009 (24)
May 2009 (16)
April 2009 (2)
March 2009 (22)
February 2009 (11)
January 2009 (11)
December 2008 (9)
November 2008 (16)
October 2008 (18)
September 2008 (11)
August 2008 (12)
July 2008 (20)
June 2008 (15)
May 2008 (27)
April 2008 (9)
March 2008 (10)
February 2008 (8)
January 2008 (8)
December 2007 (12)
November 2007 (10)
October 2007 (10)
September 2007 (6)
August 2007 (13)
July 2007 (8)
June 2007 (10)
May 2007 (12)
April 2007 (5)
March 2007 (12)
February 2007 (13)
January 2007 (22)
December 2006 (21)
November 2006 (28)
August 2006 (1)
category code (15)
food (4)
notes (4)
photo (18)
project (2)
quote (12)
sketch (13)
soup (10)
travel (2)