I am trying to write a javascript REGEX to validate a string.
It should validate to the requirement which is as follow.
it should have only Uppercase and lowercase English letters (a to z, A to Z) (ASCII: 65 to 90, 97 to 122) AND/OR Digits 0 to 9 (ASCII: 48 to 57) AND Characters - _ ~ (ASCII: 45, 95, 126). Provided that they are not the first or last character. It can also have Character. (dot, period, full stop) (ASCII: 46) Provided that it is not the first or last character, and provided also that it does not appear two or more times consecutively.
I have a Java version of the REGEX as following.
^(?=[^\W_])[\w~-]++(\.[\w*~-]++)*+(?<=[^\W_])$
And unsuccessfully trying to convert it be used with Javascript. Below is my failed attempt.
^(?=[^\W_])[\w~-]+(.[\w~-]+)(.[\w])$
it fails on the follow two tests
abc..def 1
Test cases for invalid strings:
"a." "1." "a1." "aB78." "aB78..ab" "aB78,1" "aB78 abc" ".Abc12"
Test cases for valid strings:
"a" "1" "a1" "a.1" "abc-def" "a1b2c~3" "012_345"
Based on your requirements, you could come up with:
^(?!.*\.{2})(?=.*[a-zA-Z0-9]$)[a-zA-Z0-9][-~.\w]*$
In multiline
mode, see a demo on regex101.com.
Explanation:
Collected from the Internet
Please contact [email protected] to delete if infringement.
Comments