Reputation: 23
This regex will match the first and the last character of a string:
/^.|.$/g
but why? Shouldn't it be the first 'OR' the last character? Could someone explain this to me please?
Upvotes: 1
Views: 1841
Reputation: 785481
That is matching multiple times due to use of g
(global) switch.
If you want only one match then remove g
flag:
/^.|.$/
Upvotes: 2
Reputation: 160883
/^.|.$/
matches the first or the last character, but you added the g
(global) modifier, which means you will get both.
Upvotes: 1