regex - Regular expression with alpha, hyphens, underscore and dots characters -


i've wrote regex:

/^[a-za-z\-\_\. ]{2,60}$/ 

it work fine-ish allows --- or ___ or ... or -_. entered input (without 2 alpha @ least) , don't want that. instance can have -aa, a-a, aa--- (similarly other characters).

the requirement there should @ least 2 alpha in string, , hyphens , other 2 non-alpha symbols mentioned can anywhere inside string.

use

/^(?=(?:[^a-za-z]*[a-za-z]){2})[-_. a-za-z]{2,60}$/ 

see regex demo

details:

  • ^ - start of string
  • (?=(?:[^a-za-z]*[a-za-z]){2}) - @ least 2 alpha chars in string (that is, there must 2 consecutive occurrences of:
    • [^a-za-z]* - 0 or more chars other ascii letters
    • [a-za-z] - ascii letter)
  • [-_. a-za-z]{2,60} - 2 60 occurrences of allowed chars
  • $ - end of string

note not need escape - if @ start/end of character class. _ word char, no need escaping anywhere. . not need escaping inside character class.

to tell regex engine limit ., _ , - chars max 10 in string, add (?!(?:[^._-]*[._-]){11}) negative lookahead after ^ anchor:

/^(?!(?:[^._-]*[._-]){11})(?=(?:[^a-za-z]*[a-za-z]){2})[-_. a-za-z]{2,60}$/ 

Comments

Popular posts from this blog

php - Vagrant up error - Uncaught Reflection Exception: Class DOMDocument does not exist -

vue.js - Create hooks for automated testing -

Add new key value to json node in java -