如何在3个重音之间匹配markdown块代码


  • 我知道这是一个常见的问题,但我搜索了很多,没有找到我需要的东西
  • 我试图创建一个简单的markdown插件,但我无法匹配我尝试这个regex的块代码/(^`{3}[w|W]+`{3})/gm,但它让我从第一个重音到最后一个重音,如果没有块代码,它会把它们都加在一个里面
  • 例如以下字符串
 ``` 
block code 
word1
word2
 ```
word without grave accent and it is will match it too
 ```
another block code
word1
word2
- it's will match it as part of the first block code
 ```
"""
some text after the grave accent text
word1
word2
"""`
  • 此regex结果类似于以下数组
0: "``` nblock codenword1nword2n```nnword without grave accent and it's will match it toonn```nanother block codenword1nword2n- it's will match it as part of the first block coden```"
length: 1
  • ,我希望它匹配,然后像这样
0: "``` nblock codenword1nword2n```"
1: "```nanother block codenword1nword2n- it's will match it as part of the first block coden```"
length: 2
  • 我在regex方面不够好,所以我希望有人能帮助我

您将希望延迟匹配+量词:(`{3}[w|W]+?`{3})
+?将尝试用尽可能少的字符匹配一次或多次,以确保它不会贪婪地占用`{3}s之间的所有内容。默认情况下,量词是贪婪的。

regex101示例

以下是关于懒惰与贪婪的更多阅读。

最新更新