Regex to validate a comma delimited string with a single trailing optional * but do not allow the trialing comma

I am trying to create a regex to validate a string like this:

These are all valid
A*,BC*,DEF*,HIJ,KLMN

These are all invalid
A,BC, D*F,KL**

Maximum length is 4 and only a single trailing * is permitted.

1 and 2 character strings are not permitted without a trailing *

3 character is OK – e.g. HIJ

4 character is OK as KLM* or KLMN

A-Z and the * only – plus the comma separator

No trailing comma.

I have to admit regex is beyond me.

This is as far as I have been able to get.
This examples limits it to a maximum of 10 occurrences.
This is for use in Javascript

^[^,][A-Z]+(?:,[^,]+){0,9}$*

Any help on this would be appreciated as I really struggle with regex,

I could code my way around it but that doesn’t seem a correct option.

43 thoughts on “Regex to validate a comma delimited string with a single trailing optional * but do not allow the trialing comma”

Leave a Comment