Markdown Heading in JS
Matches markdown ATX-style headings (# through ######).
Try it in the JS tester →Pattern
regexJS
^(#{1,6})\s+(.+)$ (flags: gm)JavaScript / ECMAScript code
jsJavaScript
const re = new RegExp("^(#{1,6})\\s+(.+)$", "gm");
const input = "# Title\n## Subtitle\n### Section";
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
`^(#{1,6})` captures 1–6 hash characters at line start. `\s+` requires whitespace. `(.+)$` captures the heading text to end of line.
Examples
Input
# Title
## Subtitle
### SectionMatches
# Title## Subtitle### Section
Input
####### Not a headingNo match
—