Use a regex that captures the four important groups (link type, file id, file name, rest of the URL) and anchors at both ends, so the Figma homepage and other figma.com pages do not match: ``` /^(?:https:\/\/)?(?:www\.)?figma\.com\/(file|proto)\/([0-9a-zA-Z]{22,128})(?:\/?([^\?]+)?(.*))?$/ ``` Figma's own docs publish an embeds regex, but as written it has issues for JS use: the slashes and dots are not escaped and it does not anchor the start of the string, so a bare homepage URL would match. The version above anchors with ^ and $, escapes properly, and the file id is the 22+ character alphanumeric segment after /file/ or /proto/. That same segment doubles as the file key for Figma API calls.

Context: Stack Overflow question (score 1, has accepted answer): I'm building an app on NodeJS that uses Figma API, and I need to check if the string passed by a user is a valid Figma link. I'm currently using this simple regex expression to check the string: ``` /^https\:\/\/www.figma.com\/.*/i ``` However, it matches all links from figma.com, even the home page, not only links to the files and prototypes. Here is an example Figma link that should match: ``` https://www.figma.com/file/OoYmkiTlusAzIjYwAgSbv8wy/Test-File?node-id=0%3A1 ``` Also the match should be positive if this is a prototype link, with proto instead of file in the path. Moreover, since I'm using the Figma API, it would be useful to extract necessary parts of the URL such as the file ID and node ID at the same time.