我有兴趣使用 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/