Got more questions? Find advice on: ASP | SQL | XML | Windows
in Search
Welcome to RegexAdvice Sign in | Join | Help

Michael Ash's Regex Blog

Regex Musings

Javascript Datetime regexps

Well I'm at it again. I knocked out more regexps to handle dates. Why? Why not?  Actually these are javascript compatible versions of my latest A.D calendar datetime regexps.

 I've seen a lot of comments on the regexlib complaining about expressions not working when tested with the JavaScript option.  The main reason for this is that JavaScript regex engine only supports a subset of the syntax  the VB.net version.  So an expression that works fine for VB.net may not even compile for JavaScript and some other engines if it's using some of VB's advanced syntax. The JavaScript syntax supports the basic functionality that most regex engine's, so if it works it JavaScript it should be pretty universal

 

Looking over some of my latter versions of my date regexps I've noticed that most of them use features of the more robust VB.net regex engine. Like most the more I learn about regex the more I flexed my regex writing muscles by using the advanced syntax.  The most recent date regex I wrote accepted all A.D. Dates and times from Jan 1, 0001 to Dec 31, 9999. It also captured the different date parts.  The expression used named captures, conditional groups and even had imbedded comments.  None of these features are supported by JavaScript, not yet anyway.  So the challenge to myself was to rewrite the latest version of my datetime regex that was 100% JavaScript safe.

 It wasn't as easy as I'd hoped but I finally got. 

 

These new expressions are all Javascript/JScript safe.

 

VB.net Version      Javascript Version

 

mm/dd/yyyy               mm/dd/yyyy

dd/mm/yyyy               dd/mm/yyyy

yyyy/mm/dd               yyyy/mm/dd

 

 They are functionality equivalent to their VB counterparts so refer to them for details on exactly what's being checked. These also capture the date parts in the 1st, 3rd, 4th and the time in the 5th capture groups.  While the time is always the 5th capture the date parts are captured in the order they are encountered so they depends on the format being checked.  So year is the first capture for yyyy-mm-dd format but it is the 4th capture for mm-dd-yyyy and dd-mm-yyyy formats.

 

Please see  Not Dates again for more info on dates and my other date regexps

Published Thursday, June 17, 2004 2:33 PM by mash
Filed under:

Comments

 

TrackBack said:

June 17, 2004 6:51 PM
 

TrackBack said:

June 24, 2004 2:15 AM
 

TrackBack said:

July 15, 2004 3:29 PM
 

TrackBack said:

August 5, 2004 1:21 PM
 

TrackBack said:

August 5, 2004 5:55 PM
 

TrackBack said:

August 5, 2004 10:23 PM
Anonymous comments are disabled