用于检查用户代理字符串中是否存在关键字的正则表达式 [英] Regex for checking presence and absence of keywords in User Agent String
问题描述
我在Javascript中使用正则表达式来嗅探用户代理字符串。下面是一些伪代码:
is_not_tablet_bool = /android.+mobile/i.test(navigator.userAgent.toLowerCase() );
switch(true)
{
case(is_not_tablet_bool):
return true;
休息;
}
我正在尝试制作一个可以接近相反的正则表达式以上即
- 确保'android'关键字出现在字符串中,同时
- 确保字符串中没有'mobile'关键字
感谢您的预期协助。
否定前瞻
正则表达式有一个名为负前瞻的构造,它匹配正则表达式中的字符串没有捕获前瞻部分:
你的正则表达式应该这样写:
/ android(?!。* mobile)/ i
这将匹配任何字符串包含单词 android ,后面没有单词 mobile ,忽略大小写。这也意味着您可以删除 toLowerCase
来电。
增加:负面背后隐藏
如果您只需要匹配那些字母 android 但缺少移动(在之前或之后)的字符串,则为负数组合lookaheads和lookbehinds会做。
/(?<!mobile。*)android(?!。* mobile)/ i
但问题是 Javascript不支持负面的背后隐藏。所以你必须采用一种不同的技巧来帮助你确定这种情况。
有几种可能性,其中以下似乎最有趣(最后一种有用):
-
替换之后失败的匹配否定字符串:
var nav = navigator.userAgent.replace(/mobile.*android/,fail);
返回/android(?!.*mobile)/i.test(nav);
-
使用两个前瞻。一个在普通字符串上,另一个在反向字符串上,同时也有一个反向正则表达式:
var nav = navigator.userAgent;
var after = /android(?!.*mobile)/i.test(nav);
var before = / diordna(?!。* elibom)/i.test(nav.split()。reverse()。join());
返回&&后;
-
简单是关键。两个简单的正则表达式也可以做到这一点:
var nav = navigator.userAgent;
返回/android/i.test(nav)&& !/mobile/i.test(NAV);
< blockquote>
注意:我不确定您的代码是否是实际代码,因为如果是,我强烈建议您重新考虑使用 switch(true)
语句,只需将其替换为
return is_not_tablet_bool;
。
I'm using a Regex in Javascript to sniff the User Agent string. Here is some pseudo code below:
is_not_tablet_bool = /android.+mobile/i.test(navigator.userAgent.toLowerCase());
switch(true)
{
case (is_not_tablet_bool):
return true;
break;
}
I'm trying to craft a regex that will do something close to the opposite of the above i.e.
- ensure that 'android' keyword is present in string, and at the same time
- ensure that 'mobile' keyword is absent from string
Thanks for the anticipated assistance.
Negative lookahead
Regular expressions have a construct called negative lookahead which matches a string in the regular expression without capturing the lookahead part:
Your regular expression should be written this way:
/android(?!.*mobile)/i
This will match any string that contains word android that is not followed by word mobile with ignored case. This also means that you can remove the toLowerCase
call.
Addition: Negative lookbehind
In case you need only match those strings that have word android in them but lack mobile (either before or after) then a combination of negative lookaheads and lookbehinds will do.
/(?<!mobile.*)android(?!.*mobile)/i
But the problem is that Javascript doesn't support negative lookbehinds. So you have to employ a different trick that will help you determining that situation.
There are several possibilities of which following seem to be most interesting (and last one useful):
Replace a matching negative string that will fail afterwards:
var nav = navigator.userAgent.replace(/mobile.*android/, "fail" ); return /android(?!.*mobile)/i.test(nav);
Use two lookaheads. One on normal string and the other on reversed one while also having a reversed regular expression:
var nav = navigator.userAgent; var after = /android(?!.*mobile)/i.test(nav); var before = /diordna(?!.*elibom)/i.test(nav.split("").reverse().join("")); return before && after;
Simplicity is key. Two simple regular expressions would do the trick just fine as well:
var nav = navigator.userAgent; return /android/i.test(nav) && !/mobile/i.test(nav);
Note: I'm not sure whether your code is actual code, because if it is I would strongly recommend you reconsider the use of
switch(true)
statement and simply replace it by
return is_not_tablet_bool;
.
这篇关于用于检查用户代理字符串中是否存在关键字的正则表达式的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!