I am trying to make regEx for password. (JavaScript)
[Updated]
Regex Requirements
- must be 6-18 characters consisting of english letter (capital or small), number and special character. (at least one of each)
- must start with a letter.
- the order of letters, numbers and special characters does not matter.
- does not allow Korean letters.
^(?=.*[0-9])(?=.*[-a-zA-Z])(?=.*[`~@#!$%^& =_-]){8,16}.*$
This should not pass the test in the following cases.
- It has over 16 characters.
- It contains a korean character.
However, the above mentioned cases are all passed.
Here i am sharing my regex tester link.
Thanks in advance.!
CodePudding user response:
Try this:
^(?=.{6,18}$)(?=.*\d)(?=.*[`~@#!$%^& =_-])(?=[A-Za-z][A-Za-z\d`~@#!$%^& =_-] $).*$
Edit:
The previous regex matches things like asAS12`한글
, I added $
after this part [`~@#!$%^& =_-]
, to ensure that the string ends with one of the special characters listed in []
, thanks to @Wiktor Stribiżew who mentioned this issue.
Explanation
^
the start of the line/string.(?=.{6,18}$)
ensures that the string is6-18
in long.(?=.*\d)
ensures that there must be at least one digit.(?=.*[`~@#!$%^& =_-])
ensures that there must be at least one special character listed in[]
(?=[A-Za-z][A-Za-z\d`~@#!$%^& =_-] $)
[A-Za-z]
ensures that the first character is a letter.[A-Za-z\d`~@#!$%^& =_-] $
followed by one or more letters, digits or special characters listed in the[]
no matter of the order, followed by the end of the string/line.
.*
if the above requirements are satisfied, then match the whole string.$
the end of the line/string.
See regex demo