Text Extraction
Markdown Link Regex
Inline Markdown links have two parts: the visible label in square brackets and the destination in round brackets right after it. This pattern captures the label in group 1 and the target in group 2. The label must contain at least one character, and the target cannot contain spaces or a closing bracket, which matches how most writers format links. Use it to list every link in a README, to rewrite relative paths when moving documentation, or to check that no link points to an old domain. Reference-style links and image links with a leading exclamation mark need small variations of the same idea.
Pattern (JavaScript)
/\[([^\]]+)\]\(([^)\s]+)\)/Matches the character "[", then a group (any character except \], one or more times), then the text "](", then a group (any character except )\s, one or more times), then the character ")".
Examples
Matches
- ✓ [Docs](https://example.com)
- ✓ See [home](/index) page
- ✓ [a](b)
Does not match
- ✗ [text] (url)
- ✗ plain https://example.com
- ✗ [](empty)
Try it
3 matches (JavaScript engine)
[Docs](https://example.com) See [home](/index) page [a](b) [text] (url) plain https://example.com [](empty)
Code in 8 languages
Converted automatically from the JavaScript pattern; language-specific notes are shown where the syntax or support differs.
JavaScript
const re = /\[([^\]]+)\]\(([^)\s]+)\)/g;
const text = "[Docs](https://example.com)";
const matches = [...text.matchAll(re)].map((m) => m[0]);
console.log(matches);Python
import re
pattern = re.compile(r'\[([^\]]+)\]\(([^)\s]+)\)')
text = '[Docs](https://example.com)'
matches = [m.group(0) for m in pattern.finditer(text)]
print(matches)PHP
<?php
$pattern = '/\[([^\]]+)\]\(([^)\s]+)\)/';
$text = '[Docs](https://example.com)';
preg_match_all($pattern, $text, $matches);
print_r($matches[0]);Java
import java.util.regex.Matcher;
import java.util.regex.Pattern;
public class Main {
public static void main(String[] args) {
Pattern pattern = Pattern.compile("\\[([^\\]]+)\\]\\(([^)\\s]+)\\)");
String text = "[Docs](https://example.com)";
Matcher matcher = pattern.matcher(text);
while (matcher.find()) {
System.out.println(matcher.group());
}
}
}Go
package main
import (
"fmt"
"regexp"
)
func main() {
re := regexp.MustCompile(`\[([^\]]+)\]\(([^)\s]+)\)`)
text := "[Docs](https://example.com)"
fmt.Println(re.FindAllString(text, -1))
}C#
using System;
using System.Text.RegularExpressions;
var pattern = new Regex(@"\[([^\]]+)\]\(([^)\s]+)\)");
var text = "[Docs](https://example.com)";
foreach (Match m in pattern.Matches(text))
{
Console.WriteLine(m.Value);
}Rust
// Cargo.toml: regex = "1"
use regex::Regex;
fn main() {
let re = Regex::new(r"\[([^\]]+)\]\(([^)\s]+)\)").unwrap();
let text = "[Docs](https://example.com)";
for m in re.find_iter(text) {
println!("{}", m.as_str());
}
}- Add regex = "1" under [dependencies] in Cargo.toml.
Ruby
pattern = /\[([^\]]+)\]\(([^)\s]+)\)/
text = '[Docs](https://example.com)'
matches = text.to_enum(:scan, pattern).map { Regexp.last_match[0] }
p matchesFrequently asked questions
- Does this also match image links like ?
- Yes, it matches the part starting at the bracket, because the exclamation mark sits before the match. Add (?<!!) at the start, or check the preceding character in code, if you want links only.
- Why does a space between ] and ( break the match?
- Markdown itself does not treat [text] (url) as a link. The pattern follows the same rule and requires the brackets to touch.
- Are link titles like [a](url "Title") supported?
- No. The target stops at whitespace, so the title makes the closing bracket unreachable. Extend group 2 with an optional quoted title if your documents use them.