What does this do

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • timmy_dale12@hotmail.com

    What does this do

    Im implementing a calendar and cant figure out what this method does
    or how it works

    // datetime parsing and formatting routimes. modify them if you wish
    other datetime format
    function str2dt (str_datetime) {
    var re_date = /^(\d+)\-(\d+)\-(\d+)\s+(\d+)\: (\d+)\:(\d+)$/;
    if (!re_date.exec( str_datetime))
    return alert("Invalid Datetime format: "+ str_datetime);
    return (new Date (RegExp.$3, RegExp.$2-1, RegExp.$1, RegExp.$4,
    RegExp.$5, RegExp.$6));
    }

    Mostly because of this expression RegEx.$ , which ive never seen
    before. What does it do
  • Lasse Reichstein Nielsen

    #2
    Re: What does this do

    timmy_dale12@ho tmail.com writes:
    [color=blue]
    > Mostly because of this expression RegEx.$ , which ive never seen
    > before. What does it do[/color]

    RegExp.$<n> gives you the n'th partial match of the last regualar
    expression match.
    So, if you match
    /a(.*)a(.*)a/.exec("axxxayyy ya")
    then RegExp.$1 == "xxx" and RegExp.$2 == "yyyy".

    I would prefer to use the return value of exec instead:
    if ((match = re.exec(string) ) {
    ...match[1]... match[2]...
    }

    Here match[1] refers to the same as RegExp.$1, but it is not destroyed
    by a later regular expression match.

    /L
    --
    Lasse Reichstein Nielsen - lrn@hotpop.com
    Art D'HTML: <URL:http://www.infimum.dk/HTML/randomArtSplit. html>
    'Faith without judgement merely degrades the spirit divine.'

    Comment

    • Dr John Stockton

      #3
      Re: What does this do

      JRS: In article <5bd31089.03091 80030.5ad09bca@ posting.google. com>, seen
      in news:comp.lang. javascript, timmy_dale12@ho tmail.com posted at Thu, 18
      Sep 2003 01:30:48 :-[color=blue]
      >Im implementing a calendar and cant figure out what this method does
      >or how it works
      >
      >// datetime parsing and formatting routimes. modify them if you wish
      >other datetime format
      >function str2dt (str_datetime) {
      > var re_date = /^(\d+)\-(\d+)\-(\d+)\s+(\d+)\: (\d+)\:(\d+)$/;
      > if (!re_date.exec( str_datetime))
      > return alert("Invalid Datetime format: "+ str_datetime);
      > return (new Date (RegExp.$3, RegExp.$2-1, RegExp.$1, RegExp.$4,
      >RegExp.$5, RegExp.$6));[/color]

      In addition to what Lasse wrote : the function returns a Date Object set
      to the browser's-locality date/time in the string, assuming that to be
      of the form "D-M-Y h:m:s" where DMYhms are any number >0 of digits and
      the date and time are separated by any amount of whitespace. If the
      given year is in 00..99, 1900 will be added.

      With whatever error there may be in the OS setting of Time Zone and
      Summer Time rules; depending on the application, consequences may be
      non-obvious or intermittent.

      Twice, \ before - is not needed in the RegExp.

      Any number can over/under-flow its field, so 31-9-2003 => 1-10-2003 and
      25-0-2004 is this Christmas; in other words, there is no value
      validation, only a pattern test. See via below.

      --
      © John Stockton, Surrey, UK. ?@merlyn.demon. co.uk Turnpike v4.00 IE 4 ©
      <URL:http://jibbering.com/faq/> Jim Ley's FAQ for news:comp.lang. javascript
      <URL:http://www.merlyn.demo n.co.uk/js-index.htm> JS maths, dates, sources.
      <URL:http://www.merlyn.demo n.co.uk/> TP/BP/Delphi/JS/&c., FAQ topics, links.

      Comment

      Working...