I have this regex to match username in my system: /[a-zA-Z]+([_-]?[0-9a-zA-Z]+)*$/
. It starts with letters and allows numbers, _ and -
. But I dont want the username includes some words like admin
, facebook
, official
etc. How can I add these words to the regex? I want to use one regex to manage all the constraints.
Asked
Active
Viewed 737 times
0

Joey Yi Zhao
- 37,514
- 71
- 268
- 523
-
2Can you explain why this all has to be done in just one regex instead of doing it the easy way? – Andrew Morton Mar 04 '18 at 00:31
-
`indexOf('facebook') < 0 then OK` – Ele Mar 04 '18 at 00:33
2 Answers
1
/^(?=((?!(admin|facebook|official)).)+$)[a-z]+[_-]?[0-9a-z]+$/i
Check it:
input { width: 100%; box-sizing: border-box; outline: none; }
:valid { border: 1px solid green; }
:invalid { border: 1px solid red; }
<input pattern="^(?=((?!(admin|facebook|official)).)+$)[a-zA-Z]+[_-]?[0-9a-zA-Z]+$" autofocus>

Qwertiy
- 19,681
- 15
- 61
- 128