JavaScript / ECMAScript

Markdown Image in JS

Matches Markdown image syntax ![alt](url).

Try it in the JS tester →

Pattern

regexJS
!\[([^\]]*)\]\(([^)]+)\)   (flags: g)

JavaScript / ECMAScript code

jsJavaScript
const re = new RegExp("!\\[([^\\]]*)\\]\\(([^)]+)\\)", "g");
const input = "![logo](https://example.com/logo.png)";
const matches = [...input.matchAll(re)];
console.log(matches.map(m => m[0]));

Uses `String.prototype.matchAll` for global iteration (Node 12+ / all modern browsers).

How the pattern works

`!\[([^\]]*)\]` captures the alt text inside brackets. `\(([^)]+)\)` captures the URL in parentheses.

Examples

Input

![logo](https://example.com/logo.png)

Matches

  • ![logo](https://example.com/logo.png)

Input

![](image.jpg) and ![alt](pic.gif)

Matches

  • ![](image.jpg)
  • ![alt](pic.gif)

Same pattern, other engines

← Back to Markdown Image overview (all engines)