I\'m looking for a regex that will check if the string only consists of the letters a-z, numbers, underscore (_
) and hyphen (-
). I have tried this,
The answers so far correctly explain the character class /^[A-Za-z0-9_-]+$
/
You can check the length. + means "one or more of the preceding", * means zero or more of the preceding" and {2,4} means "between 2 and 4 of the preceding".
You can't specify the length has to be a prime, though. Specifying that the length has to be even is possible but non-trivial: /^(xx){1,3}$
/ matches xx, xxxx or xxxxxx. The count here refers to the number of pairs, not the number of x'es