AI Content Creation

Extract a URL From Text or a Hyperlink in Google Sheets

The regex answer almost works. It returns a link with a comma stuck on the end, which is the same as returning nothing.

By FITS TeamAugust 3, 20267 min read

You have a column of pasted notes, email bodies, or scraped snippets. Each cell has a link somewhere inside it and you want that link in its own column.

Two different problems hide under this one question, and most guides only answer the easier one.

The Old Way (REGEXEXTRACT)

Every result you find gives you this pattern.

=REGEXEXTRACT(A2, "https?://[^\s]+")

It reads as match everything after the protocol until a space. Now try it on a sentence a human actually wrote.

Check docs (https://example.com/api?v=1&ref=abc), or email support.

The formula returns https://example.com/api?v=1&ref=abc), with the closing bracket and comma attached. Paste that into a browser or feed it to IMPORTDATA and it fails. The cell looks correct at a glance, which is what makes this one expensive.

Trailing punctuation is not fixable by tightening the pattern either. A closing bracket is a legal URL character, so the regex cannot tell a bracket in the path from a bracket that closes the sentence.

The second problem is worse. If the cell shows the word Website and the link was attached with Ctrl+K, the formula returns #N/A.

That is not a pattern bug. Cell formulas only see the visible text value, never the rich text metadata underneath. Most top-ranking pages conflate these two cases and leave you debugging a pattern that was never the problem. The next section solves the hyperlink case properly.

Extract a URL From a Hyperlink in Google Sheets

There are two kinds of hyperlink in Google Sheets and they need different answers. If the cell was built with a =HYPERLINK() formula, one formula pulls the address out. If the link was attached with Ctrl+K, the URL lives in rich text metadata that no formula can read, and you need a short Apps Script function.

Test which one you have with a single cell. Put this next to the link.

=ISFORMULA(A2)

TRUE means it is a =HYPERLINK() cell, so use the formula below. FALSE on a cell that still renders blue and underlined means it is a Ctrl+K rich text link, so use the script.

If the cell uses =HYPERLINK()

FORMULATEXT returns the formula itself as a string, and the URL is the first quoted argument inside it. Pull that out.

=REGEXEXTRACT(FORMULATEXT(A2), """([^""]+)""")

On =HYPERLINK("https://example.com/pricing", "Pricing") this returns https://example.com/pricing. The doubled quotes are how you write a literal quote character inside a Sheets string, and they are the part people get wrong. If the URL is stored in another cell rather than typed inline, FORMULATEXT gives you the reference instead of the address, so point the formula at that source cell directly.

Extract the Hyperlink From a Cell With Apps Script

Ctrl+K links are the case every regex answer fails on. Open Extensions > Apps Script, paste this, save, then use it like any other function.

function LINKURL(a1) {
  const cell = SpreadsheetApp.getActiveSheet().getRange(a1);
  const rich = cell.getRichTextValue();
  if (!rich) return "";
  if (rich.getLinkUrl()) return rich.getLinkUrl();
  for (const run of rich.getRuns()) {
    if (run.getLinkUrl()) return run.getLinkUrl();
  }
  return "";
}
=LINKURL("A2")

Three details decide whether this works. The reference goes in quotes, because a custom function receives the cell value and not the cell itself, so passing a bare A2 would hand the script the word Website instead of the address. The getRuns() loop matters when only part of the text carries the link, which is common in pasted email bodies. And custom functions cache their result, so a link edited later will not refresh until you retype the formula.

Once the URL is in a plain cell you can treat it like any other string, including extracting the domain from the URL.

The FITS Way (Ask for the Link)

For links that live in the visible text, FITS handles the punctuation problem because it reads the sentence rather than scanning characters.

=FITS("Extract the URL from this text. Return the URL only, with no trailing punctuation. Return blank if there is no URL: " & A2)

The bracket and comma are sentence punctuation, so they do not come back. The query string does, because it is part of the address.

Cells often hold several links. Say which one you want.

=FITS("This text may contain several links. Return only the first one that is not a social media profile: " & A2)

Bare domains with no protocol are the other common miss, since the regex requires http to match at all.

=FITS("Return any web address in this text, including bare domains written without http. Add https:// if the protocol is missing: " & A2)

And you can strip it down in the same pass instead of nesting a second formula.

=FITS("Extract the URL from this text and return it without query parameters or tracking tags: " & A2)

One caveat worth being straight about. If the URL is hidden behind Ctrl+K rich text, no formula can see it, including this one. Use the LINKURL script above to lift those addresses into plain cells first.

When to Use Each

If your cells contain nothing but a bare URL, REGEXEXTRACT is fine and costs nothing. Reach for =FITS() when links sit inside sentences, when punctuation surrounds them, or when a cell holds more than one. Related jobs work the same way: extracting the domain once you have the URL, pulling data from a website into Sheets, and extracting an email address from messy text. The full tour is in automating Google Sheets tasks you used to need regex for.

Common Questions

How do I get the URL out of a hyperlink in Google Sheets?

Check =ISFORMULA(A2) first. If it returns TRUE the cell is a =HYPERLINK() formula and =REGEXEXTRACT(FORMULATEXT(A2), """([^""]+)""") returns the address. If it returns FALSE the link was added with Ctrl+K and lives in rich text, which needs the LINKURL Apps Script function above.

Why does REGEXEXTRACT return #N/A on my hyperlink?

Because the cell value is the display text, not the link. A cell showing Website has the literal value Website, and the URL sits in separate rich text metadata that formulas never receive. Tightening the pattern cannot fix this, since there is no URL in the string being matched.

Can I extract hyperlinks from a whole column at once?

Yes, but the reference has to change per row. Use =LINKURL("A" & ROW()) and fill it down, which builds the A1 string for each row. Custom functions do not accept a range and return an array here, so one formula per row is the working pattern.

Why does my extracted URL have a comma or bracket on the end?

The standard pattern matches until whitespace, and sentence punctuation is not whitespace. A closing bracket is also a legal URL character, so no regex can reliably tell a bracket in the path from one that closes the sentence. That is the case =FITS() handles, because it reads the sentence rather than scanning characters.

Does the LINKURL script update when the link changes?

No. Google Sheets caches custom function results and only recalculates when an argument changes. The argument here is the text string A2, which does not change when the link behind the cell does. Retype the formula or edit the cell to force a refresh.

Get Links That Actually Open

FITS puts plain-English AI formulas inside Google Sheets. Describe the link you want and get it clean, without the trailing comma. Free tier included.