1

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

  • [all the things][things] => "things"
  • [something] => "something"

但不是:

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

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

说得通?谢谢!

4

1 回答 1

1

例如:

/(?:\[[\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
于 2013-08-24T08:07:28.870 回答