One possible approach is to call validateChunk function in a loop:
for (var i = 0, l = chunks.length; i < l; i++) {
if (! validateChunk(chunks[i])) {
return false;
}
}
return true;
As a sidenote, there's exactly zero sense to use .match
when .test
clearly suffices (as you don't have to collect the results of match). So I'd rewrite the function like this:
function validateChunk(str) {
return /^[a-z]+(?:\(\d+\))?$/.test(str);
}
Finally, can't help noticing that all this validation can be done with a single regex:
function testCases(str)
{
var pattern = /^[a-z]+(?:\(\d+\))?(?:,[a-z]+(?:\(\d+\))?)*$/
return pattern.test(str);
}
Actually, judging from your description, the pattern should be slightly different: first, _
symbol is valid one and should be included to character class; second, the same class should either include A-Z
range as well, or instead a pattern should be given /i
modififer. This demo includes all these changes.