const regex = /(^\|(?<tableSeparator>[\s\-|]+)\|\n)|(^\|\s*(?<tableTopFirst>[^|]+)(?=.*\n\|\s*-))|((?<!^)\|(?<tableTopMiddle>[^|\n]+)(?=\|.*\|\n\| -))|(\|(?<tableTopLast>[^|]+)\|\n(?=\|\s*-))|(^\|\s*(?<tableBottomFirst>[^|]+)(?=.*\n^[^|]))|((?<!^)\|(?<tableBottomMiddle>[^|\n]+)(?=\|.*\|\n[^|]))|(\|\s*(?<tableBottomLast>[^|]+)\|\n(?!\|))|(^\|\s*(?<tableMiddleFirst>[^|]+)(?=\|))|(\|\s*(?<tableMiddleLast>[^|]+)\|\n)|(\|\s*(?<tableMiddleMiddle>[^|]+))|!?(\[(?<text>[^(]+)]\((?<url>[^")]+))("(?<title>[^"]+)"|.*)\)|((?<!-.+\n)- (?<unorderedListFirstItem>.+)\n)|((?<=-.+\n)- (?<unorderedListMiddleItem>.+)\n(?=-))|(- (?<unorderedListLastItem>.+)\n(?!-))|((?<!\d+\..+\n)\d+\. (?<orderedListFirstItem>.+)\n)|((?<=\d+\..+\n)\d+\. (?<orderedListMiddleItem>.+)\n(?=\d+\.))|(\d+\. (?<orderedListLastItem>.+)\n(?!\d+\.))|(\*\*\*(?<boldAndItalic>[^*]*)\*\*\*)|(\*\*(?<bold>[^*]*)\*\*)|(\*(?<italic>[^*]*)\*)|(~~(?<strikethrough>[^~]*)~~)|(`{3}\n(?<multilineCode>[^`]+)`{3}\n)|(`(?<inlineCode>[^`\n]*)`)|(^#{6} (?<h6>.+))|(^#{5} (?<h5>.+))|(^#{4} (?<h4>.+))|(^#{3} (?<h3>.+))|(^#{2} (?<h2>.+))|(^# (?<h1>.+))|(?<horizontalLine>^-{3}\n)|(?<=^\n)> (?<singleLineQuote>.+)\n(?!^>)|((?<!> .+\n)> (?<quoteFirst>.+))|(> (?<quoteLast>.+)(?!.*\n> .+))|(> (?<quoteMiddle>.+))|(?<lineBreak>\n{2})/gm;
// Alternative syntax using RegExp constructor
// const regex = new RegExp('(^\\|(?<tableSeparator>[\\s\\-|]+)\\|\\n)|(^\\|\\s*(?<tableTopFirst>[^|]+)(?=.*\\n\\|\\s*-))|((?<!^)\\|(?<tableTopMiddle>[^|\\n]+)(?=\\|.*\\|\\n\\| -))|(\\|(?<tableTopLast>[^|]+)\\|\\n(?=\\|\\s*-))|(^\\|\\s*(?<tableBottomFirst>[^|]+)(?=.*\\n^[^|]))|((?<!^)\\|(?<tableBottomMiddle>[^|\\n]+)(?=\\|.*\\|\\n[^|]))|(\\|\\s*(?<tableBottomLast>[^|]+)\\|\\n(?!\\|))|(^\\|\\s*(?<tableMiddleFirst>[^|]+)(?=\\|))|(\\|\\s*(?<tableMiddleLast>[^|]+)\\|\\n)|(\\|\\s*(?<tableMiddleMiddle>[^|]+))|!?(\\[(?<text>[^(]+)]\\((?<url>[^")]+))("(?<title>[^"]+)"|.*)\\)|((?<!-.+\\n)- (?<unorderedListFirstItem>.+)\\n)|((?<=-.+\\n)- (?<unorderedListMiddleItem>.+)\\n(?=-))|(- (?<unorderedListLastItem>.+)\\n(?!-))|((?<!\\d+\\..+\\n)\\d+\\. (?<orderedListFirstItem>.+)\\n)|((?<=\\d+\\..+\\n)\\d+\\. (?<orderedListMiddleItem>.+)\\n(?=\\d+\\.))|(\\d+\\. (?<orderedListLastItem>.+)\\n(?!\\d+\\.))|(\\*\\*\\*(?<boldAndItalic>[^*]*)\\*\\*\\*)|(\\*\\*(?<bold>[^*]*)\\*\\*)|(\\*(?<italic>[^*]*)\\*)|(~~(?<strikethrough>[^~]*)~~)|(`{3}\\n(?<multilineCode>[^`]+)`{3}\\n)|(`(?<inlineCode>[^`\\n]*)`)|(^#{6} (?<h6>.+))|(^#{5} (?<h5>.+))|(^#{4} (?<h4>.+))|(^#{3} (?<h3>.+))|(^#{2} (?<h2>.+))|(^# (?<h1>.+))|(?<horizontalLine>^-{3}\\n)|(?<=^\\n)> (?<singleLineQuote>.+)\\n(?!^>)|((?<!> .+\\n)> (?<quoteFirst>.+))|(> (?<quoteLast>.+)(?!.*\\n> .+))|(> (?<quoteMiddle>.+))|(?<lineBreak>\\n{2})', 'gm')
const str = `# Table
| Syntax test | Description | Test | XXX | XXX |
| ----------- | ----------- | ---- | --- | --- |
| Header | Title | AAAA | XXX | XXX |
| Header | Title | AAAA | XXX | XXX |
| Paragraph | Text | BBBB | XXX | XXX |
## Pretty table
|Header 1|Header 2|
|---|---|
|||
### Lists
---
- Inline *code* \`aaaaz\`
- My **favorite search** engine is ~~not~~ [Duck Duck Go](https://duckduckgo.com "Click me").
- ~~***Recursive***~~
- ***~~Recursive reversed~~***
- Item 4
1. Item
2. Item
3. Item
4. Item
1. c
1. c
#### Multiline code
\`\`\`
int method() {
return 2137;
}
\`\`\`
##### Quotes
> aa
> aa
> > bb
> > bb
> > > [c](#panes)c
> > > cc
> > bb
> > bb
> aa
> aa
> single line
`;
// Reset `lastIndex` if this regex is defined globally
// regex.lastIndex = 0;
let m;
while ((m = regex.exec(str)) !== null) {
// This is necessary to avoid infinite loops with zero-width matches
if (m.index === regex.lastIndex) {
regex.lastIndex++;
}
// The result can be accessed through the `m`-variable.
m.forEach((match, groupIndex) => {
console.log(`Found match, group ${groupIndex}: ${match}`);
});
}
Please keep in mind that these code samples are automatically generated and are not guaranteed to work. If you find any syntax errors, feel free to submit a bug report. For a full regex reference for JavaScript, please visit: https://developer.mozilla.org/en/docs/Web/JavaScript/Guide/Regular_Expressions