Skip to content

Instantly share code, notes, and snippets.

@paulonteri
Created August 23, 2023 16:06
Show Gist options
  • Save paulonteri/88ae562e6685479206379cb880fe7f48 to your computer and use it in GitHub Desktop.
Save paulonteri/88ae562e6685479206379cb880fe7f48 to your computer and use it in GitHub Desktop.

You can use this regular expression to match markdown links:

\[(.*?)\]\(.*?\)

Explanation:

  • \[ and \] match a literal open and close bracket.
  • (.*?) matches any character (.), repeated any number of times (*) but as few as possible (?). The brackets create a capture group.
  • \( and \) match a literal open and close parenthesis.
  • .*? matches any character, repeated any number of times but as few as possible.

Then you can use the first capture group $1 to replace the matched link with the text content.

In JavaScript, you can apply this regex using str.replace() function:

let str = "[personal information you disclose to us](https://www.notion.so/667877d1f5a1485ca078dbffc3ad42c3?pvs=21)."
let newStr = str.replace(/\[(.*?)\]\(.*?\)/g, "$1");

In the example above, newStr will be:

"personal information you disclose to us."

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment