Markdown Image Regex

!\[([^\]]+)\]\(([^)]+)\)

Extract markdown images ![alt](src) from documents.

Test This Pattern →

Pattern Breakdown

Understanding the regex pattern !\[([^\]]+)\]\(([^)]+)\):

  • ^ - Start of string
  • + - One or more
  • [...] - Character class
  • (...) - Capturing group

Examples

Input Result Explanation
![Alt text](image.png) ✓ Match Standard image
![](empty-alt.jpg) ✓ Match Empty alt text

Code Examples

JavaScript

JavaScript
const pattern = /!\[([^\]]+)\]\(([^)]+)\)/;
const text = "![Alt text](image.png)";

// Test if string matches
if (pattern.test(text)) {
  console.log("Valid!");
}

// Extract matches
const matches = text.match(pattern);
console.log(matches);

Python

Python
import re

pattern = r'!\[([^\]]+)\]\(([^)]+)\)'
text = "![Alt text](image.png)"

# Test if string matches
if re.match(pattern, text):
    print("Valid!")

# Find all matches
matches = re.findall(pattern, text)
print(matches)

PHP

PHP
<?php
$pattern = '/!\[([^\]]+)\]\(([^)]+)\)/';
$text = "![Alt text](image.png)";

// Test if string matches
if (preg_match($pattern, $text)) {
    echo "Valid!";
}

// Find all matches
preg_match_all($pattern, $text, $matches);
print_r($matches);
?>

Java

Java
import java.util.regex.*;

String pattern = "!\\[([^\\]]+)\\]\\(([^)]+)\\)";
String text = "![Alt text](image.png)";

Pattern p = Pattern.compile(pattern);
Matcher m = p.matcher(text);

if (m.matches()) {
    System.out.println("Valid!");
}

Use Cases

Common applications: Image extraction, CDN migration

Frequently Asked Questions

What is the regex pattern for markdown image regex?
The regex pattern is: !\[([^\]]+)\]\(([^)]+)\). Extract markdown images ![alt](src) from documents.
How do I use the markdown image regex pattern in JavaScript?
In JavaScript: const regex = new RegExp('!\\[([^\\]]+)\\]\\(([^)]+)\\)'); or const regex = /!\[([^\]]+)\]\(([^)]+)\)/;
Does this pattern work in Python?
Yes, use: import re; pattern = re.compile(r'!\[([^\]]+)\]\(([^)]+)\)') in Python. Most regex patterns are cross-language compatible.
What are common use cases for markdown image regex?
Image extraction, CDN migration

Related Regex Patterns

More Regex Tools

GetDevUtils offers a suite of developer tools including: