我有一个带有markdown的字符串。我正在尝试使用正则表达式删除所有markdown,但是在匹配链接时遇到了麻烦。这是我走了多远:
function stripMarkdown(text) {
var str = String(text).replace(/(__|\*|\#)/gm, '');
return str;
}
var testStr = '# This is the title. ## This is the subtitle. **some text** __some more text__. [link here](http://google.com)'
stripMarkdown(testStr);
Run Code Online (Sandbox Code Playgroud)
因此,我相信以上内容将删除除链接之外的所有不必要的降价。我该如何处理?另外,如果有更好的方法可以做到这一点,请告诉我。
期望的结果:
This is the title. This is the subtitle. some text some more text. link here
Run Code Online (Sandbox Code Playgroud)
接受的答案与粗体标签*
和标题相匹配###
。如果一行上有多个括号对,马文的修复会匹配奇怪的文本组。(例如[word] a [link](url)
)
此正则表达式修复了:
.replace(/\[([^\[\]]*)\]\((.*?)\)/gm, '$1')
Run Code Online (Sandbox Code Playgroud)
请注意,其中包含括号对的 URL 需要进行 URL 编码
我想出了这个正则表达式:
(?:__|[*#])|\[(.*?)\]\(.*?\)
Run Code Online (Sandbox Code Playgroud)
(?:__|[*#])|\[(.*?)\]\(.*?\)
Run Code Online (Sandbox Code Playgroud)
归档时间: |
|
查看次数: |
4947 次 |
最近记录: |