问题描述
- 我知道这是一个常见问题,但我搜索了很多但没有找到我需要的
- 我正在尝试创建简单的降价插件,但我无法匹配块代码我尝试过这个正则表达式
/(^\`{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
"""`
- 这个正则表达式的结果就像下面的数组
0: "``` \nblock code\nword1\nword2\n```\n\nword without grave accent and it's will match it too\n\n```\nanother block code\nword1\nword2\n- it's will match it as part of the first block code\n```"
length: 1
- 我希望它像这样匹配
0: "``` \nblock code\nword1\nword2\n```"
1: "```\nanother block code\nword1\nword2\n- it's will match it as part of the first block code\n```"
length: 2
- 我不太擅长正则表达式,所以我希望有人帮助我
解决方法
您需要懒惰地匹配您的 (\`{3}[\w|\W]+?\`{3})
量词:+?
\`{3}
将尝试用尽可能少的字符匹配一次或多次,确保它不会贪婪地占用 {{1}} 之间的所有内容。默认情况下,量词是贪婪的。
这里有一些关于懒惰与贪婪的more reading。