Advertisement
If you have a new account but are having problems posting or verifying your account, please email us on hello@boards.ie for help. Thanks :)
Hello all! Please ensure that you are posting a new thread or question in the appropriate forum. The Feedback forum is overwhelmed with questions that are having to be moved elsewhere. If you need help to verify your account contact hello@boards.ie

Help with Javascript regular expression

Options
  • 13-11-2008 12:40pm
    #1
    Registered Users Posts: 7,405 ✭✭✭


    I'm trying to create a Javascript regular expression that will accept the following

    RER or rer followed by a letter [A,B,C,or no letter] followed by [0-9]{1,9}

    I hope that make sense,

    So far I have

    var filter = /^([R,r])+([E,e])+([R,r])+([A,a,B,c,C,c])+([0-9]{1,9})$/;

    I just don't know how to get it to recognise if there is no letter following the RER, i.e I can't get it to recognise RER1

    Any help much appreciated


Comments

  • Registered Users Posts: 7,405 ✭✭✭fletch


    Think I have it, added a question mark to the filter
    var filter = /^([R,r])+([E,e])+([R,r])+([A,a,B,c,C,c]?)+([0-9]{1,9})$/;


  • Registered Users Posts: 21,264 ✭✭✭✭Hobbes


    /^rer[A-C]?\d{1,9}/ig


  • Registered Users Posts: 1,393 ✭✭✭Inspector Gadget


    If you're okay with the regex matching odd combinations of cases (such as 'ReRa1') then Hobbes' regex is fine, with a couple of slight modifications so that it can be use to retrieve the match:

    /^(rer[A-C]?\d{1,9})$/ig

    If you need it to match either exactly 'RER' or 'rer', then a letter between 'A' and 'C' (either case), and then between one and nine digits, try this:

    /^((?:RER|rer)[A-Ca-c]?\d{1,9})$/ig

    Gadget


  • Registered Users Posts: 21,264 ✭✭✭✭Hobbes


    /^(rer[A-C]?\d{1,9})$/ig

    That one will only work if the line is only the requested match. I took it to mean he is looking for all lines beginning with.
    /^((?:RER|rer)[A-Ca-c]?\d{1,9})$/ig

    Second one won't work because your setting it to ignore case. So it will still match stuff like "rEr" for example. To fix that you can remove the "i". Again it is an exact line match only. To stop that you remove the "$".

    If the match is needed not at the start of the line then remove the "^".


  • Registered Users Posts: 1,393 ✭✭✭Inspector Gadget


    Hobbes wrote: »
    Second one won't work because your setting it to ignore case. So it will still match stuff like "rEr" for example. To fix that you can remove the "i". Again it is an exact line match only. To stop that you remove the "$".
    Yep, typo on my part :o

    Gadget


  • Advertisement
Advertisement