admin管理员组文章数量:1419242
I need to match Safari browsers, but not Chrome browsers using one Regex. I usually don't have a problem with regex, but negative look-aheads are stumping me here.
/(?!chrome)(safari)/i.test("Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/536.5 (KHTML, like Gecko) Chrome/19.0.1084.56 Safari/536.5");
Can't figure out why this is returning true
.
I need to match Safari browsers, but not Chrome browsers using one Regex. I usually don't have a problem with regex, but negative look-aheads are stumping me here.
/(?!chrome)(safari)/i.test("Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/536.5 (KHTML, like Gecko) Chrome/19.0.1084.56 Safari/536.5");
Can't figure out why this is returning true
.
-
You're matching a substring
safari
which is not equal tochrome
. (Not very meaningful if I understand it correctly.) – pimvdb Commented Jun 19, 2012 at 20:53 - Hmm, I'm probably misinterpreting the use. I'm trying to have the string match safari, but only if the string doesn't match chrome. – dtbarne Commented Jun 19, 2012 at 20:57
-
Negative lookahead:
q(?!u)
matches aq
that is not followed by au
. That won't help here. You actually need a negative lookbehind for this job (string containsSafari
that is not preceded byChrome
) but JS does not support negative lookbehind. – Matt Ball Commented Jun 19, 2012 at 21:00
1 Answer
Reset to default 5This will do it
^(?!.*chrome).*(safari)
BTW JavaScript does not support lookbehind but I see no problem with lookahead.
What you are saying with
(?!chrome)(safari)
Is that right before "safari" (zero width so starting with the cursor before s) there cannot be the string "chrome" which doesn't make sense.
本文标签: javascriptRegex Negative Lookahead for Chrome amp SafariStack Overflow
版权声明:本文标题:javascript - Regex Negative Lookahead for Chrome & Safari - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1745301467a2652396.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论