[英]How do I extract code from markdown code block string?
If I have this string like this如果我有这样的字符串
```
console.log()
```
Hello
or或者
Hello
```
console.log()
```
or或者
```console.log()``` Hello
or或者
Hello ```console.log()```
How do I get just the console.log() string?如何仅获取 console.log() 字符串?
---------------------------------------------- Edit ---------------------------------------------- - - - - - - - - - - - - - - - - - - - - - - - 编辑 - - ------------------------------------------
The regex I use is the combination between regex answered by @Himanshu Tanwar with the one suggested by @ASDFGerte我使用的正则表达式是@Himanshu Tanwar 回答的正则表达式与@ASDFGerte 建议的正则表达式的组合
var code = s.match(/```([^`]*)```/)[1]
You can try doing it with regular expression您可以尝试使用正则表达式
var s = "```console.log()```Hello";
var code = s.match(/```(.*)```/)[1]
Look at follow regex, it's match all your strings:看看下面的正则表达式,它匹配你所有的字符串:
/```[^`]*```/gmi
Working example can be found here https://regex101.com/r/hcRumt/1工作示例可以在这里找到https://regex101.com/r/hcRumt/1
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.