What does this do

Discussion in 'Javascript' started by timmy_dale12@hotmail.com, Sep 18, 2003.

  1. Guest

    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
     
    , Sep 18, 2003
    #1
    1. Advertising

  2. writes:

    > Mostly because of this expression RegEx.$ , which ive never seen
    > before. What does it do


    RegExp.$<n> gives you the n'th partial match of the last regualar
    expression match.
    So, if you match
    /a(.*)a(.*)a/.exec("axxxayyyya")
    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 -
    Art D'HTML: <URL:http://www.infimum.dk/HTML/randomArtSplit.html>
    'Faith without judgement merely degrades the spirit divine.'
     
    Lasse Reichstein Nielsen, Sep 18, 2003
    #2
    1. Advertising

  3. JRS: In article <>, seen
    in news:comp.lang.javascript, posted at Thu, 18
    Sep 2003 01:30:48 :-
    >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));


    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.demon.co.uk/js-index.htm> JS maths, dates, sources.
    <URL:http://www.merlyn.demon.co.uk/> TP/BP/Delphi/JS/&c., FAQ topics, links.
     
    Dr John Stockton, Sep 18, 2003
    #3
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Bill Johnson
    Replies:
    0
    Views:
    1,244
    Bill Johnson
    Jul 8, 2005
  2. Replies:
    3
    Views:
    496
    Ken Cox [Microsoft MVP]
    Oct 7, 2005
  3. Janet Collins
    Replies:
    0
    Views:
    1,161
    Janet Collins
    Jan 13, 2006
  4. Replies:
    1
    Views:
    863
    Juan T. Llibre
    Feb 8, 2006
  5. Alek Nazarian
    Replies:
    7
    Views:
    3,252
    Alek Nazarian
    Oct 22, 2003
Loading...

Share This Page