Javascript equivalent of Java Regex

A Gilani

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"

Jan

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

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

Equivalent java regex string for this

From Dev

JavaScript regex \G + offset equivalent

From Dev

Java Deflater Equivalent in Javascript

From Dev

Equivalent regex pattern from c# to javascript

From Dev

PHP Regex (.*?) and callback \\1 JavaScript equivalent

From Dev

Equivalent regex pattern from c# to javascript

From Dev

What is the equivalent of javascript setTimeout in Java?

From Dev

is there a Vector Java class equivalent in javascript?

From Dev

Is there a Java equivalent of the Javascript method fromCharCode()?

From Dev

Javascript file object equivalent in java

From Dev

Is there a Java equivalent of the Javascript method fromCharCode()?

From Dev

What is the equivalent JavaScript regex for this php regex boundary set?

From Dev

Is there a Java equivalent of JavaScript var a = b || c

From Dev

Javascript equivalent of Java's matcher.matches

From Dev

What is the equivalent of "integer" from java in javascript?

From Dev

Javascript equivalent of Java's matcher.matches

From Dev

What is the equivalent of Regex-replace-with-function-evaluation in Java 7?

From Dev

Javascript equivalent to $.on

From Dev

Javascript += equivalent?

From Dev

Convert RegEx from JavaScript to Java

From Dev

Convert Java tokenizing regex into Javascript

From Dev

How to convert a JavaScript regex into Java?

From Dev

Python regex equivalent for perl

From Java

“===” equivalent in Java

From Dev

“===” equivalent in Java

From Dev

Is Javascript constructor function equivalent/similar to a class or interface in Java

From Dev

What is the equivalent of Java `Collection.addAll` for JavaScript arrays?

From Dev

java string.getBytes("UTF-8") javascript equivalent

From Dev

Javascript/NodeJS equivalent code for the Java code Cipher.doFinal(byte[])?

Related Related

  1. 1

    Equivalent java regex string for this

  2. 2

    JavaScript regex \G + offset equivalent

  3. 3

    Java Deflater Equivalent in Javascript

  4. 4

    Equivalent regex pattern from c# to javascript

  5. 5

    PHP Regex (.*?) and callback \\1 JavaScript equivalent

  6. 6

    Equivalent regex pattern from c# to javascript

  7. 7

    What is the equivalent of javascript setTimeout in Java?

  8. 8

    is there a Vector Java class equivalent in javascript?

  9. 9

    Is there a Java equivalent of the Javascript method fromCharCode()?

  10. 10

    Javascript file object equivalent in java

  11. 11

    Is there a Java equivalent of the Javascript method fromCharCode()?

  12. 12

    What is the equivalent JavaScript regex for this php regex boundary set?

  13. 13

    Is there a Java equivalent of JavaScript var a = b || c

  14. 14

    Javascript equivalent of Java's matcher.matches

  15. 15

    What is the equivalent of "integer" from java in javascript?

  16. 16

    Javascript equivalent of Java's matcher.matches

  17. 17

    What is the equivalent of Regex-replace-with-function-evaluation in Java 7?

  18. 18

    Javascript equivalent to $.on

  19. 19

    Javascript += equivalent?

  20. 20

    Convert RegEx from JavaScript to Java

  21. 21

    Convert Java tokenizing regex into Javascript

  22. 22

    How to convert a JavaScript regex into Java?

  23. 23

    Python regex equivalent for perl

  24. 24

    “===” equivalent in Java

  25. 25

    “===” equivalent in Java

  26. 26

    Is Javascript constructor function equivalent/similar to a class or interface in Java

  27. 27

    What is the equivalent of Java `Collection.addAll` for JavaScript arrays?

  28. 28

    java string.getBytes("UTF-8") javascript equivalent

  29. 29

    Javascript/NodeJS equivalent code for the Java code Cipher.doFinal(byte[])?

HotTag

Archive