识别以下内容:
[stack][overflow]
or
[is great!]
我愿意:
/\[([^\]]+)\](\[([^\]]+)\])?/.match(s)
现在我想扩展它,如果 [...][...]
或 [...]
出现在 \
它不会被识别:
\[stack][overflow] # => Should not match
\[is great!] # => Should not match
a \[b][c] \[d] [e] # => Should match [e]
如何扩展正则表达式来实现此目的?
它应该可以在 Ruby 1.9.2 和 Ruby 1.8.7 中运行。
最佳答案
我想出了这个正则表达式:
/([^\\\[\]]|^)\[([^\]]+)\](\[([^\]]+)\])?/
您可以在这里尝试:
http://rubular.com/r/L796M2pcG7
希望有帮助。
编辑:对您的评论使用react 编辑:下一个
关于Ruby 正则表达式匹配除非用\转义,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7401244/