Why consecutive lookaheads do not always work

pom421

I want a regex which returns true when there is at least 5 characters et 2 digits. For that, I use a the lookahead (i. e. (?=...)).

// this one works
let pwRegex = /(?=.{5,})(?=\D*\d{2})/;
let result = pwRegex.test("bana12");

console.log("result", result) // true

// this one won't
pwRegex = /(?=.{5,})(?=\d{2})/;
result = pwRegex.test("bana12");

console.log("result", result) // false

Why we need to add \D* to make it work ?

For me, \d{2} is looser than \D*\d{2} so it should not allow an acceptance of the test?

SamWhan

Your lookaheads only test from the current match position. Since you don't match anything, this means from the start. Since bana12 doesn't start with two digits, \d{2} fails. Its as simple as that ;)

Also, note that having \d{2} means your digits has to be adjacent. Is that your intention?

To simply require 2 digits, that doesn't need to be adjacent, try

/(?=.{5,})(?=\D*\d\D*\d)/

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

Consecutive mouseenter functions do not work

From Dev

Why does this JavaScript not always work?

From Dev

Why does my php `if` not always work?

From Dev

Why does question mark not always work in iPython?

From Dev

Why does ToString() not always work on DateTime objects?

From Dev

Why doesn't the preventDefault function work always?

From Dev

Why does my php `if` not always work?

From Dev

Why bindings do not work?

From Dev

Why do ^ and $ not work as expected?

From Dev

Are mysql inserts are always consecutive?

From Dev

Intra-package imports do not always work

From Dev

Comparison of 2 tables do not always work

From Dev

bashrc volume changes do not always work

From Dev

Why do I have to always specify the range in STL's algorithm functions explicitly, even if I want to work on the whole container?

From Dev

Why do I always have to change the position of a sprite to something else before I can get the desired position to work

From Dev

Why do icon fonts always use the PUA?

From Dev

Why do I always get 0?

From Dev

Why do the default Applications in Ubuntu always change?

From Dev

Why do these expressions always return 7?

From Dev

Why future example do not work?

From Dev

Why my lambdas do not work?

From Dev

Why getElementsByClassName do not work in Mozilla?

From Dev

Input comparison does not work properly, always going to the failure case, why?

From Dev

why process substitution does not always work with while loop in bash?

From Dev

Why doesn't increment operator work on chars always?

From Dev

Why ObjectAnimator.cancel() doesn't always work?

From Dev

Why doesn't increment operator work on chars always?

From Dev

Why doesn't file_get_conents always work?

From Dev

indexPath.row always gives zero (why not work?)

Related Related

  1. 1

    Consecutive mouseenter functions do not work

  2. 2

    Why does this JavaScript not always work?

  3. 3

    Why does my php `if` not always work?

  4. 4

    Why does question mark not always work in iPython?

  5. 5

    Why does ToString() not always work on DateTime objects?

  6. 6

    Why doesn't the preventDefault function work always?

  7. 7

    Why does my php `if` not always work?

  8. 8

    Why bindings do not work?

  9. 9

    Why do ^ and $ not work as expected?

  10. 10

    Are mysql inserts are always consecutive?

  11. 11

    Intra-package imports do not always work

  12. 12

    Comparison of 2 tables do not always work

  13. 13

    bashrc volume changes do not always work

  14. 14

    Why do I have to always specify the range in STL's algorithm functions explicitly, even if I want to work on the whole container?

  15. 15

    Why do I always have to change the position of a sprite to something else before I can get the desired position to work

  16. 16

    Why do icon fonts always use the PUA?

  17. 17

    Why do I always get 0?

  18. 18

    Why do the default Applications in Ubuntu always change?

  19. 19

    Why do these expressions always return 7?

  20. 20

    Why future example do not work?

  21. 21

    Why my lambdas do not work?

  22. 22

    Why getElementsByClassName do not work in Mozilla?

  23. 23

    Input comparison does not work properly, always going to the failure case, why?

  24. 24

    why process substitution does not always work with while loop in bash?

  25. 25

    Why doesn't increment operator work on chars always?

  26. 26

    Why ObjectAnimator.cancel() doesn't always work?

  27. 27

    Why doesn't increment operator work on chars always?

  28. 28

    Why doesn't file_get_conents always work?

  29. 29

    indexPath.row always gives zero (why not work?)

HotTag

Archive