Advertisement
Help Keep Boards Alive. Support us by going ad free today. See here: https://subscriptions.boards.ie/.
If we do not hit our goal we will be forced to close the site.

Current status: https://keepboardsalive.com/

Annual subs are best for most impact. If you are still undecided on going Ad Free - you can also donate using the Paypal Donate option. All contribution helps. Thank you.
https://www.boards.ie/group/1878-subscribers-forum

Private Group for paid up members of Boards.ie. Join the club.

Help with Javascript regular expression

  • 13-11-2008 12:40PM
    #1
    Registered Users, Registered Users 2 Posts: 7,460 ✭✭✭


    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, Registered Users 2 Posts: 7,460 ✭✭✭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, Registered Users 2 Posts: 21,264 ✭✭✭✭Hobbes


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


  • Registered Users, Registered Users 2 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, Registered Users 2 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, Registered Users 2 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