Reputation: 151
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!
Upvotes: 0
Views: 405
Reputation: 163467
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 stringconst 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",
"KEPM39,JEMGH5,HEPM21",
"JEPM34, KEPM11 ",
"JEPM34, KEPM11",
"JEPM34",
"KEPM39, JEMGH5 HEPM21, HEGD44 ZZZZZZ",
"KEPM39, JEMGH5 HEPM21"
].forEach(s =>
console.log(`${s} ==> ${validate(s)}`)
);
Upvotes: 2
Reputation: 75910
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.
Upvotes: 3