SitePoint Sponsor

User Tag List

Results 1 to 4 of 4
  1. #1
    38911 Basic Bytes Free johnuk's Avatar
    Join Date
    Jul 2008
    Location
    Somerset, England
    Posts
    459
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    RegEx Number Ranges

    Hi everyone!

    I am trying to lnclude the number ranges 1 through 15 in a regular expression I am writing for my website.

    I have been doing some reading on number ranges but I cant quite work it out, any help would be much appreciated!

    kind regards,
    John

  2. #2
    SitePoint Guru
    Join Date
    Apr 2006
    Posts
    802
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    If the value is the entire string use test-
    if(/^(1[0-5]|[1-9])$/.test(str);

    If you are seaching through a long string for possible multiple instances of the pattern use exec:

    Code:
    var Rx=/(^|\D)(1[0-5]|[1-9])(\D|$)/g;
    var str='There are 14 days in 2 weeks and 21 days in 3 weeks';
    
    var M, A=[];
    while((M=Rx.exec(str))!=null){
    	A.push(M[2]);
    }
    alert(A);// return A or handle any processing in the loop

  3. #3
    38911 Basic Bytes Free johnuk's Avatar
    Join Date
    Jul 2008
    Location
    Somerset, England
    Posts
    459
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I really cant thankyou enough MrHoo !!!

    very kind of you to help.

    kind regards

  4. #4
    38911 Basic Bytes Free johnuk's Avatar
    Join Date
    Jul 2008
    Location
    Somerset, England
    Posts
    459
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    hi again!

    That solution worked great!
    Last edited by johnuk; Jul 16, 2008 at 05:20.


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
  •