What is the regex to match alphanumeric 6 character words, separated by space or comma

I am newbie in RegEx and trying to design a RegEx which could match the String like below:

pattern 1 separated by comma and a space: KEPM39, JEMGH5, HEPM21 … (repeat)

pattern 2 separated only by a space: KEPM39 JEMGH5 HEPM21 … (repeat)

pattern 3 separated only by a comma: KEPM39,JEMGH5,HEPM21 … (repeat)

this is my concept: "^[a-zA-Z0-9]{6,}[,\s]+$" but it seems wrong.

#I want to validate the whole string, and I use javascript & html to validate user input. (textarea)

#duplicate change to repeat to be more suitable.

function validate(){
    var term = "JEPM34, KEPM11 ";
    var re = new RegExp("^[a-zA-Z0-9]{6,}[,\s]+$");
    if (re.test(term)) {
        return true
    } else {
        return false

thanks you in advance!

26 thoughts on “What is the regex to match alphanumeric 6 character words, separated by space or comma”

  1. A very loose way to validate could be:

    ^[A-Z\d]{6}(?:[ ,]+[A-Z\d]{6})*$

    See the online demo. With loose, I meant that [ ,]+ is not checking that each delimiter in your string is the same per definition. Therefor even "KEPM39, JEMGH5 HEPM21, HEGD44 ZZZZZZ" would be valid.

  2. If you want consistent delimiters, and there can be trailing spaces (as there is in your example data) you can use a capture group with a backreference \1 to keep consistent delimiters and match optional spaces at the end.

    Note that you can also use \s but that could also match a newline.

    Using test will return a boolean, so you don’t have to use return true or false but you can return the result test`

    ^[A-Z\d]{6}(?:(, ?| )(?:[A-Z\d]{6}\1)*[A-Z\d]{6} *)?$

    The pattern matches:

    • ^ Start of string
    • [A-Z\d]{6} Match 6 occurrences of a char A-Z or a digit
    • (?: Non capture group to match as a whole
      • (, ?| ) Capture group 1, match either a comma and optional space, or a space to be used as a backreference
      • (?:[A-Z\d]{6}\1)* Optionally repeat any of the listed followed by a backreference \1 to group 1 which will match the same delimiter
      • [A-Z\d]{6} * Match any of the listed and optional spaces at the end
    • )? Close the group and make it optional to also match an instance without delimiters
    • $ End of string

    Regex demo

    const regex = /^[A-Z\d]{6}(?:(, ?| )(?:[A-Z\d]{6}\1)*[A-Z\d]{6} *)?$/;
    const validate = term => regex.test(term);
      "KEPM39, JEMGH5, HEPM21",
      "KEPM39 JEMGH5 HEPM21",
      "JEPM34, KEPM11 ",
      "JEPM34, KEPM11",
      "KEPM39, JEMGH5 HEPM21, HEGD44    ZZZZZZ",
      "KEPM39, JEMGH5 HEPM21"
    ].forEach(s =>
      console.log(`${s} ==> ${validate(s)}`)

Leave a Comment