bevacqua
bevacqua

Reputation: 48476

regex issue in javascript

"foo = '@test.bar';\nfooa = @test.darn;".match(/@([a-z][a-z\.-_]*)/igm)

Why does this match

["@test.bar", "@test.darn;"]

rather than just

["@test.bar", "@test.darn"]

?

Upvotes: 1

Views: 39

Answers (1)

Bergi
Bergi

Reputation: 664237

In character classes, some letters have special meanings. The dot for example has none and does not need to be escaped. The minus in contrast defines a range of characters, and if you mean literally minus you need to escape it or put it in the end/beginning of the character class. Your range from . to _ actually includes ./0123456789:;<=>?@ABCDEFGHIJKLMNOPQRSTUVWXYZ[\\]^_. You may want to use [a-z.\-_] or [a-z._-] instead.

Upvotes: 5

Related Questions