1

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"

4
  • js regex is mostly the same as any other regex. what have you tried? Commented Mar 21, 2016 at 17:03
  • What did you try to convert it to JS? Show us your unsuccessful attempt! Commented Mar 21, 2016 at 17:03
  • it may be easier to use non-capturing group ?: Commented Mar 21, 2016 at 17:11
  • ^(?=[^\W_])[\w~-]+(\.[\w~-]+)*(.*[a-zA-Z0-9])$ is my unsuccessful attempt. It failes on the abc...def Commented Mar 21, 2016 at 17:12

2 Answers 2

3

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:

  • ^ assure it's the beginning of the line
  • (?!.*.{2,}) negative lookahead - no two dots consecutively
  • (?=.*[a-zA-Z0-9]$) the last character needs to be one of these
  • [a-zA-Z0-9] first character needs to be one of these
  • [-~.\w]* anything of these afterwards (zero or more times)
  • $ assure the string ends here
Sign up to request clarification or add additional context in comments.

5 Comments

This works perfectly. Thank you for taking time to explain the regex as well.
The expression fails for a single character and 2 character string. e.g a 1 a1 1a
@AGilani: Please clarify your requirements then and let's see what we can do.
Please accept apologies for not asking the question in full details. Just added new items to passing and failing test scenarios. Appreciate your help.
@AGilani: Updated the answer, see regex demo. Using multiple lookaheads now.
2

There are no lookbehinds in JavaScript regexes. Fortunately, you didn't need it in your case anyway. Replace

(?<=[^\W_])

With just

[^\W_]

1 Comment

i came to the same conclusion and rewrote it as follow ^(?=[^\W_])[\w~-]+(\.[\w~-]+)*(.*[a-zA-Z0-9])$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.