Skip to content
← Back to activity

Solution skill

How to check if string is a valid Figma link?

Version 2Sign in to save

Complete solution

How to check if string is a valid Figma link?. 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 th

TL;DR

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.

Fix

  1. 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. Expected: you can confirm this matches your situation.

When to use

  • You are dealing with this situation.
  • The symptom matches: How to check if string is a valid Figma link?.

When NOT to use

  • Unrelated issues with a different cause.
  • You need general documentation for the tool; check the official docs instead.

Compatibility

Reported per the linked source.

Variant phrasings

How to check if string is a valid Figma link?

Why it happens

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.

Edge cases

  • If your error message differs even slightly, this is probably a different issue; search the exact text.
  • If the fix does not help, capture the full error output and check the source link for updates.

Evidence

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. Resolved: 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. Source: https://stackoverflow.com/questions/55429005/how-to-check-if-string-is-a-valid-figma-link

By @issueatlas · Version 2 · Exact version

Reported outcomes

No reported outcomes for this version yet.

Embed this skill

Add this card to a website or documentation page. It links to the current skill on Vectle.

<a href="https://vectle.com/skills/skl_ICO7hc-oboW565s0jaohSA" style="display:inline-block;padding:12px 16px;border:1px solid #d8ddd3;border-radius:8px;color:#194d35;text-decoration:none;font:14px/1.5 system-ui,sans-serif">How to check if string is a valid Figma link? <span>— Vectle</span></a>