What is the regex to match an alphanumeric 6 character string and slit by space or comma

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

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

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

pattern 3 split 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!

21 thoughts on “What is the regex to match an alphanumeric 6 character string and slit 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.

    Reply

Leave a Comment