[英]Match string unless it is inside square brackets
I'm trying to adapt the a JQuery zen coding plug in I found to my own uses. 我正在尝试将我发现的JQuery zen编码插件改编成自己的用途。 One of the issues I'm having is a bug inside the code where something like 我遇到的问题之一是代码中的错误,例如
a[href="google.com"]
will expand to 将扩展到
<a href="google.com" class="com"</a>
The regex being used by the code is: 该代码使用的正则表达式为:
/(\.[\w-]+)/gi
Which I have modified with a look-ahead to see if there is an even number of brackets in front of it. 我已对它进行了修改,以查看前面是否有偶数个括号。
/(\.[\w-]+)(?=([^\]]*\][^\]]*\])*[^\]]*$)/gi
I've used regex testers and it seems to work, but when using javascript .match()
, it is returning ".com" as a match inside the above string. 我使用过正则表达式测试器,它似乎可以正常工作,但是使用javascript .match()
,它会在上述字符串中返回“ .com”作为匹配项。
The below response has solved the bug in question but has created a new one. 以下回复解决了该错误,但创建了一个新错误。 Now: 现在:
a.class[href="google.com"]
is not resolving to 不解决
http://jsfiddle.net/sA9sQ/ http://jsfiddle.net/sA9sQ/
EDIT: I accepted the answer below because it got me where I needed to go. 编辑:我接受下面的答案,因为它使我到了我需要去的地方。 The final regex turned out to be /(\\.[\\w-]+)(?=([^\\]]*\\][^\\]]*\\])*[^\\]]*\\.[\\w-]+)/gi
最终的正则表达式为/(\\.[\\w-]+)(?=([^\\]]*\\][^\\]]*\\])*[^\\]]*\\.[\\w-]+)/gi
尝试将前瞻移至正则表达式的前面,如下所示:
/(?=([^\]]*\][^\]]*\])*[^\]]*$)(\.[\w-]+)/gi
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.