Regular expression problem

Discussion in 'JavaScript' started by _marcus, Sep 5, 2008.

  1. #1
    Hello (my first entry) !

    I have a problem with regular expression. I am trying to specify a pattern to make sure the inputted data is one word (string of characters ranging from A-Za-z - never mind the ÅÄÖ and the åäö (Swedish signs)) followed by a space and a number with 1 to 4 digits.

    My current code:

    pattern = /^([A-ZÅÄÖa-zåäö])+([\s]?)+([\d{1,4}])+$/;

    I have also tried (which is proposingly the same thing):

    pattern = = /^([A-ZÅÄÖa-zåäö])+([\s]?)+([0-9]{1,4})+$/;


    Unfortunately this does not work. It does make sure I have a space and atleast one digit but it does not limit the number to have at most 4 digits.

    I have searched for some time and I can´t find any good example to a solution. Most articles of regular expressions in javascript seem to be theoretical. But according to javascriptkit.com () I should use the quantifier {n,m} to "{n,m} matches n to m times".

    Solution?
     
    _marcus, Sep 5, 2008 IP
  2. _marcus

    _marcus Peon

    Messages:
    2
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #2
    Sometimes the answer is right in front of you. All you need to do is ask someone else to realize it yourself:

    Solution: Remove the last '+'.

    pattern = = /^([A-ZÅÄÖa-zåäö])+([\s]?)+([0-9]{1,4})$/;


    Thanks anyway!
     
    _marcus, Sep 5, 2008 IP
  3. temp2

    temp2 Well-Known Member

    Messages:
    1,231
    Likes Received:
    5
    Best Answers:
    0
    Trophy Points:
    150
    Digital Goods:
    2
    #3
    temp2, Sep 5, 2008 IP