Regex Not Capturing Certain File Extensions in JavaScript - Unexpected Matches with Complex Patterns
Quick question that's been bugging me - I'm working on a script that should identify specific file extensions in a string, but I'm running into an unexpected scenario with my regex pattern. I'm trying to match file names that end with either `.jpg`, `.png`, or `.gif` but not those that are preceded by a URL or path component. For example, it should match `image.jpg` and `photo.png`, but not `https://example.com/images/image.jpg`. Here's the regex pattern I've been using: ```javascript const regex = /(?<!https?:\/\/[^\s]*)\b(\w+\.\(jpg|png|gif)\b/; ``` However, when I test with the string `Check out this image: image.jpg, and this one: https://example.com/images/image.jpg`, it incorrectly matches `image.jpg` and `https://example.com/images/image.jpg` together which is not what I expected. I've tried tweaking the negative lookbehind part of the regex, but I'm not getting the desired results. The current failure message I see is `Uncaught SyntaxError: Invalid regular expression: /(?<!https?:\/\/[^\s]*)\b(\w+\.\(jpg|png|gif)\b/: Nothing to look behind for`. Iām using Node.js version 14.15.4 which supports lookbehinds. Can someone guide to understand why this regex isn't working as intended and how to fix it? Any insights on improving my regex for this scenario would be greatly appreciated. My development environment is Linux. Is there a better approach?