HTML Tag Matcher in JS
Match paired HTML tags and capture the tag name and inner content using a back-reference.
Try it in the JS tester →Pattern
regexJS
<([a-zA-Z][a-zA-Z0-9]*)\b[^>]*>([\s\S]*?)<\/\1> (flags: g)JavaScript / ECMAScript code
jsJavaScript
const re = new RegExp("<([a-zA-Z][a-zA-Z0-9]*)\\b[^>]*>([\\s\\S]*?)<\\/\\1>", "g");
const input = "<p>Hello world</p>";
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
Group 1 captures the tag name. [^>]* matches attributes. [\s\S]*? lazily captures inner content. \1 back-references the opening tag name to ensure the closing tag matches.
Examples
Input
<p>Hello world</p>Matches
<p>Hello world</p>
Input
<div class="box">content</div>Matches
<div class="box">content</div>