const regex = /(^([#]+|[*-+]\.|[0-9]+\.|[ ]{3,}|[> ]+)\s([^\n]+)$|([*_~\-]+)([^\4]+?)\4|(`+)([^\n]+)?\n([^\6]+?)\6)/gm;
// Alternative syntax using RegExp constructor
// const regex = new RegExp('(^([#]+|[*-+]\\.|[0-9]+\\.|[ ]{3,}|[> ]+)\\s([^\\n]+)$|([*_~\\-]+)([^\\4]+?)\\4|(`+)([^\\n]+)?\\n([^\\6]+?)\\6)', 'gm')
const str = `# 标题
引用
> 缩进
>> 多级缩进
> > 呵呵
普通文本 *加粗文本* ***特别粗的文本*** ~删除~ _斜体_
\`\`\`JavaScript
Code
\`\`\`
另一个代码段
\`\`\`
Code
\`\`\`
## 二级标题
1. 有序 列表一
2. 有序列表二
3. 有序列表三
*. 无序列表一
*. 无序列表二`;
// 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