I know that with JS RegExp I can match:
- Exact length;
/^[a-zA-Z]{7}$/ - A length range;
/^[a-zA-Z]{3,7}$/ - A minimum length;
/^[a-zA-Z]{3,}$/
What if I wanna match varying specific lengths? Something like: /^[a-zA-Z]{2|4|6}$/ meaning that the string must have either length of 2, 4 or 6 characters. Can I do it using JavaScript's RegExp?
I tried googling but couldn't find a way to search for this specific case, I always end up finding how to match length ranges.
Scenario: I currently have this if statement that I would like to make shorter:
if(typeof ean !== 'string' || !/^\d+$/.test(ean) || [8, 12, 13, 14].indexOf(ean.length) === -1) {
return false;
}
^[a-zA-Z]{2}(?:[a-zA-Z]{2}(?:[a-zA-Z]{2})?)?$instead of an alternation./^(?:[a-zA-Z]{2}){1,3}$/. But this isn't a good, clean solution; it's a clever one.