!\[([^\]]*)\]\(([^)\s]+)\) This pattern matches Markdown images of the form , capturing the alt text (which may be empty) and the image source URL. The leading ! distinguishes images from ordinary links. Web patterns deal with markup, styling and web identifiers: HTML tags and comments, CSS colors, Markdown links, slugs and source-code comments. They are handy for quick scanning and lightweight transforms, but markup is not a regular language, so anything structural or security-sensitive should use a real parser (DOM, CSS or Markdown) rather than regex alone. It uses the g flag(s). Apply it across text to collect every occurrence. Alt text may be empty, so the group uses * rather than +; ordinary links require non-empty text. It does not handle reference-style images or titles; extend for full Markdown coverage. URLs with spaces or parentheses need escaping that this simple class does not support. As with any regular expression, confirm its behavior in your target engine, test boundary and adversarial inputs, and watch for catastrophic backtracking before relying on it in production.
Token-by-token explanation
! — a literal exclamation marking an image\[([^\]]*)\] — capture alt text, allowing empty ([])\(([^)\s]+)\) — capture the image URL with no spacesg flag — find all images in the document
What it matches
 What it does not match
[link](url)![alt] (src)![alt] When to use it
Reach for a web pattern for quick scanning and lightweight transforms of markup, styling and web identifiers: locating tags, comments, colors, slugs or Markdown links. For anything structural or security-sensitive — parsing a DOM, sanitizing HTML, rewriting CSS — use a real parser or a proven library instead, because markup is not a regular language and regex-based handling is a frequent source of bugs and cross-site scripting bypasses.
Caveats
- Alt text may be empty, so the group uses * rather than +; ordinary links require non-empty text.
- It does not handle reference-style images or titles; extend for full Markdown coverage.
- URLs with spaces or parentheses need escaping that this simple class does not support.
Code examples
const re = /!\[([^\]]*)\]\(([^)\s]+)\)/g;
const text = "see  and more";
const found = [...text.matchAll(re)].map(m => m[1] ?? m[0]);
// found holds every extracted occurrence import re
rx = re.compile(r"!\[([^\]]*)\]\(([^)\s]+)\)")
text = "see  and more"
found = rx.findall(text) # every occurrence (groups if present) import java.util.regex.*;
Pattern p = Pattern.compile("!\\[([^\\]]*)\\]\\(([^)\\s]+)\\)");
Matcher m = p.matcher("");
boolean ok = m.find(); // scan for occurrences <?php
$subject = "";
if (preg_match_all('/!\[([^\]]*)\]\(([^)\s]+)\)/', $subject, $m)) {
// $m[0] is the full match; $m holds every occurrence
} How to test it
Before trusting any pattern, test it against three kinds of input: valid examples that must match, invalid examples that must be rejected, and edge cases at the boundaries (empty strings, maximum lengths, unusual separators and Unicode). Paste the expression into a live regex tester with your actual sample data and confirm the highlighted matches are exactly what you intend. Watch for catastrophic backtracking by trying long non-matching inputs, and remember that engine differences (JavaScript, Python, Java, PCRE, Go/RE2) mean a pattern that works in one may fail or behave differently in another — especially lookarounds, which Go's RE2 does not support at all.
Frequently asked questions
Why is alt text allowed to be empty?
Accessibility best practice permits empty alt for decorative images, so  is valid; the * quantifier captures zero or more characters accordingly.
How is this different from a Markdown link?
An image has a leading ! before the brackets. Without it, the syntax is a link; the ! is what tells the renderer to embed an image instead.
When should I avoid regex for web content?
For parsing HTML, CSS or Markdown structure, use a real parser. Regex cannot handle nesting, quoted delimiters or malformed markup reliably, and regex-based sanitization is a common source of XSS bypasses.
Why is regex HTML sanitization dangerous?
HTML has nested, quoted and malformed constructs that regex mis-parses, so attackers hide scripts in forms the pattern misses. Always sanitize with a proven library (DOMPurify) or a real parser, never ad-hoc regex.
Try the Regex Tester
Live regex matching with groups, flags and explanations.