www.webdeveloper.com
Results 1 to 2 of 2

Thread: regex {x,y} not working as I expected

Hybrid View

  1. #1
    Join Date
    Apr 2013
    Posts
    2

    regex {x,y} not working as I expected

    {x,y} Matches x to y number of occurrences of a regular expression. e.g. /\d{2,4}/ matches at least 2 but no more than 4 digits.


    Code:
    var re = /\d{2,4}/;
    document.write(re.test('12345'));
    This code outputs, true. Because the string contains 5 digits, shouldn't this output false?

  2. #2
    Join Date
    Feb 2006
    Posts
    2,927
    You are finding 4 digits, as requested by your pattern.

    If you want to restrict the match to strings that only contain 2-4 digits and nothing else, flag the beginning and end of the string-
    Code:
    var re=/^\d{2,4}$/;

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Posting Permissions

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



Recent Articles