SitePoint Sponsor

User Tag List

Results 1 to 5 of 5

Thread: regexp please

  1. #1
    SitePoint Wizard boxhead's Avatar
    Join Date
    Feb 2004
    Location
    UK
    Posts
    1,040
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    regexp please

    I need a regepr to validate a year range e.g. 2004-05 in the format yyyy-yy.

    Also, is it possible to validate that yy is 1 year greater than yyyy so 2000-02 would fail?

    cheers

    monkey
    monkey - the rest is history

  2. #2
    SitePoint Wizard
    Join Date
    Mar 2001
    Posts
    3,537
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    /\d\d\d\d-\d\d/

    is it possible to validate that yy is 1 year greater than yyyy so 2000-02 would fail?
    Not that I can figure. But, you could manually check the matches and see which ones comply with that rule. split() the match at the '-', get the last two characters of the first piece using string methods, turn it into a number, turn the second piece into a number, compare.

  3. #3
    SitePoint Member
    Join Date
    Apr 2005
    Posts
    9
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I agree with 7stud.
    I don't see any way to compare values with regexp.

    The purpose of regexp's are pattern matching : it certifies that you have the right sequence of characters in the right place, nothing more.

    another pattern proposal :
    /\d{4}-\d{2}/

  4. #4
    Floridiot joebert's Avatar
    Join Date
    Mar 2004
    Location
    Kenneth City, FL
    Posts
    823
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    If back references within a pattern were negatable it might be possible to compare.

    Either of theese functions would still be shorter than the pattern required though...
    Code:
    var dte1 = '2004-05';
    var dte2 = '2004-06';
    
    function rightDate1(s){
    	return /^\d{4}-\d{2}$/.test(s) && parseInt(s.substring(2,4)) === parseInt(s.substring(5,7))-1;
    }
    
    function rightDate2(s){
    	return /^\d{4}-\d{2}$/.test(s) && eval(s.substring(2,7)) === -1;
    }
    
    alert('rightDate1\n' + dte1 + ' : ' + rightDate1(dte1) + '\n' + dte2 + ' : ' + rightDate1(dte2) );
    alert('rightDate2\n' + dte1 + ' : ' + rightDate2(dte1) + '\n' + dte2 + ' : ' + rightDate2(dte2) );
    I like the second one personally, an actual use for eval for once.
    Last edited by joebert; Nov 26, 2005 at 10:51. Reason: Forgot pattern anchors

  5. #5
    SitePoint Wizard boxhead's Avatar
    Join Date
    Feb 2004
    Location
    UK
    Posts
    1,040
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Cheers all for the great responses!
    monkey - the rest is history


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •