twopluszero / next-images

Import images in Next.js (supports jpg, jpeg, svg, png and gif images)
MIT License
949 stars 67 forks source link

File format regex doesn't properly escape first period #77

Closed danoc closed 3 years ago

danoc commented 3 years ago

Hello!

I think the regex on this line isn't working as intended:

https://github.com/twopluszero/next-images/blob/3095b206c804c205f101ac8c01c6c3ca8f9c4093/index.js#L24

In particular, the \. doesn't require that the file extension start with a period.

Here's an example.

const extensions = ["png", "jpg", "jpeg", "gif", "ico", "image\\.svg"];

const regex = new RegExp(`\.(${extensions.join('|')})$`)

console.log(regex.test("foo.png")); // true, correct behavior
console.log(regex.test("foo.svg")); // false, correct behavior
console.log(regex.test("foo_image.svg")); // true, **incorrect behavior**
console.log(regex.test("foo.image.svg")); // true, correct behavior

Notice how foo_image.svg is considered a match even though there's an underscore, not a period, before the word image.

The issue goes away if another forward slash is added to the RegExp line as such:

new RegExp(`\\.(${extensions.join('|')})$`)

I'd be happy to send a PR if you'd like! 🙇🏻

arefaslani commented 3 years ago

Hey @danoc, that would be nice :) Thank you!