What’s the difference between /^(?=.*[w])$/
and /^(?=.*[w])
.
/^(?=.*[w])$/.test('a')
returnfalse
/^(?=.*[w])/.test('a')
returntrue
.
Why is that?
What’s the difference between /^(?=.*[w])$/
and /^(?=.*[w])
.
/^(?=.*[w])$/.test('a')
return false
/^(?=.*[w])/.test('a')
true
.Why is that?
2
Answers
Basically
/^(?=.*[w])$/ -> $/ -> takes the entire string for matching – if yes – return true
/^(?=.*[w]). -> Here it checks for the first character matching – if yes – return true
This pattern asserts a word character, but the match is an empty string
^$
so that will always be false:This pattern asserts a word character, which will be true for
a
, but also for a string likeab %
If you want to combine them and get a match for only
a
you can assert a word character followed by the end of the string inside the assertion:But in that case you don’t need an assertion, you can just match: