javascript - 如何匹配引用风格的 Markdown 链接?

标签 javascript

我有兴趣使用 Javascript 查找文本字符串中的所有引用样式 Markdown 链接。所以我想要以下内容:

  • [所有事物][事物] =>“事物”
  • [某事] =>“某事”

但不是:

  • [o hai](http://example.com)
  • [o hai] (http://example.com)

换句话说,一个左方括号后面跟着一个右方括号,捕获里面的文本,但后面跟着一组括号。

有道理吗?谢谢!

最佳答案

例如:

/(?:\[[\w\s]*\])?(\[[\w\s]*\])(?!\s*\()/
 ^--------------^ - possibly first [...], ?: - non-capturing group
                 ^-----------^ - followed by [...]
                              ^-------^ - not followed by "   (" - spaces + (

> [all the things][things]".match(/(?:\[[\w\s]*\])?(\[[\w\s]*\])(?!\s*\()/)[1]
"[things]"
> "[something]".match(/(?:\[[\w\s]*\])?(\[[\w\s]*\])(?!\s*\()/)[1]
"[something]"
> "[o hai](http://example.com)".match(/(?:\[[\w\s]*\])?(\[[\w\s]*\])(?!\s*\()/)
null
> "[o hai] (http://example.com)".match(/(?:\[[\w\s]*\])?(\[[\w\s]*\])(?!\s*\()/)
null

关于javascript - 如何匹配引用风格的 Markdown 链接?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18416409/

相关文章:

javascript - 删除双击缩放 D3

javascript - 替换嵌套数组中的对象

javascript - 如何提高 Monocle 电子阅读器库的滚动速度?

javascript - Chrome 扩展覆盖

javascript - Python/BeautifulSoup 与 JavaScript 源

javascript - 每个 session 的 Google Analytics 事件都是唯一的

javascript - 如何使用数据表在单击按钮时显示带有列数据的确认模态

php - 表单在 php 验证之前提交

javascript - 有人可以解释为什么这个正则表达式与不同的正则表达式风格匹配不同吗?

javascript - 错误 "Property ' type' is missing in type 'typeof WithXXX' "