Need a regex that will check if the string only consist letters a-z and numbers and underscore(_) and hyphen(-)

前端 未结 5 477
甜味超标
甜味超标 2020-12-31 10:39

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,

5条回答
  •  南方客
    南方客 (楼主)
    2020-12-31 11:06

    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

提交回复
热议问题