I have this example:
/(?=\d)(?=[a-z])/.test("3a")
which returns false
but this
/(?=\d)(?=.*[a-z])/.test("3a")
works.
Can you explain this?
Let me break down what you are doing:
Test string = test("3a")
Example 1: /(?=\d)(?=[a-z])/
(?=\d)
is a positive lookahead that the next character is a digit
(?=[a-z])
is a positive lookahead that the next character is in range a-z
This is impossible and will always return false as it is asserting that the next character is both a-z and a digit which it cannot be.
Example 2: /(?=\d)(?=.*[a-z])/
(?=\d)
is a positive lookahead that the next character is a digit
(?=.*[a-z])
is a positive lookahead that anywhere in your string after where match starts there is a character is in range a-z
This sees 3a
in the test string because starting the match at 3 the next character is a digit and 3a fulfills the .*[a-z]
assertion.
It may or may not be important to point out that because these are lookaheads you are not actually matching anything. I don't know what it is you are really trying to do.
If you want to test that there is a-z after a digit you can put it into one assertion:
/(?=\d[a-z])/