Quick JS Regex required!

Discussion in 'JavaScript' started by Cobnut, Jan 30, 2012.

  1. #1
    Hi, can some kind soul help me with a quick RegEx for Javascript?

    I need a test against the following pattern: ABCNNNXX(X)

    In plain English, where ABC is a specific three alpha characters, e.g. "DAB", NNN is any three digits [0-9], and XX(X) are a minimum of any 2 alphas, up to a maximum of 3. So, if "DAB" is my specific starting 3 letters...

    "DAB018JG" would match
    "DAB01JHGT" would not (not enough numbers, too many alphas at end)
    "DBA018JHG" would not (DAB bit wrong)

    I'm sure it's pretty simple but I just can't get it to work.

    Jon
     
    Cobnut, Jan 30, 2012 IP
  2. JohnnySchultz

    JohnnySchultz Peon

    Messages:
    277
    Likes Received:
    4
    Best Answers:
    7
    Trophy Points:
    0
    #2
    try this..
     /[A-Z]{3}[0-9]{3}[A-Z]{2,3}/ 
    Code (markup):
     
    JohnnySchultz, Jan 31, 2012 IP
  3. Cobnut

    Cobnut Peon

    Messages:
    184
    Likes Received:
    6
    Best Answers:
    0
    Trophy Points:
    0
    #3
    Thanks Johnny, that's kind of how I thought it would be, but wouldn't that allow any three characters for the first bit, rather than a specific string?

    I may do better to split the string initially to test these first three chars absolutely, then regex the remainder...

    Thanks again...
     
    Cobnut, Feb 1, 2012 IP
  4. JohnnySchultz

    JohnnySchultz Peon

    Messages:
    277
    Likes Received:
    4
    Best Answers:
    7
    Trophy Points:
    0
    #4
    your welcome
     
    JohnnySchultz, Feb 2, 2012 IP