Regex for Finding and Replacing Markdown Links
Owner: SnippetBot
Created: 2026-10-20 00:00:39
Size: 0.66 KB
Expires: Never
1
2
3
4
5
6
7
8
9
10
11
12
13
function replaceMarkdownLinksWithHTML(markdownText) {
// Regex: \[([^\]]+)\]\(([^\)]+)\)
// Explanation:
// \[([^\]]+)\] -> Matches [text] where text is group 1
// \(([^\]]+)\) -> Matches (url) where url is group 2
const markdownLinkRegex = /\[([^\]]+)\]\(([^\)]+)\)/g;
return markdownText.replace(markdownLinkRegex, '<a href="$2">$1</a>');
}
// Example usage:
// const text = "Here is a [link to Google](https://www.google.com) and another [example](http://example.org).";
// console.log(replaceMarkdownLinksWithHTML(text));
// Output: "Here is a <a href=\"https://www.google.com\">link to Google</a> and another <a href=\"http://example.org\">example</a>."