在编写正则表达式以获取浏览器和版本的过程中。这就是我到目前为止所拥有的。任何人都可以建议更好的过程吗?正则表达式将检查IE / Firefox / Safari / Chrome,我可以查看版本号。
import flashx.textLayout.formats.BackgroundColor;
var inputField:TextField = new TextField();
var displayField:TextField = new TextField();
// RegExp
var reg:RegExp = /(MSIE|(?!Gecko.+)Firefox|(?!AppleWebKit.+Chrome.+)Safari|(?!AppleWebKit.+)Chrome|AppleWebKit(?!.+Chrome|.+Safari)|Gecko(?!.+Firefox) (SeaMonkey))(?: |\/)([\d]+)/i;
inputField.border = true;
inputField.width = 500;
inputField.height = 50;
inputField.x = 75;
inputField.y = 50;
inputField.type = "input";
inputField.multiline = true;
addChild(inputField);
displayField.border = true;
displayField.width = 500;
displayField.height = 200;
displayField.backgroundColor = 0x666666;
displayField.x = 75;
displayField.y = inputField.y + 50 + inputField.height;
displayField.multiline = true;
addChild(displayField);
inputField.addEventListener(Event.CHANGE, changeListener, false, 0, true);
function changeListener(event:Event):void
{
var strToTest:String = inputField.text;
var textToDisplay:String = "";
if(reg.test(strToTest))
{
var browser:String = strToTest.match(reg)[1];
var version:int = strToTest.match(reg)[2];
var chromeVersion:int = 29;
var firefoxVersion:int = 23;
var ieVersion:int = 7;
var safariVersion:int = 6;
textToDisplay += "BROWSER IS " + browser;
textToDisplay += "\nVersion " + version;
if(browser == "Chrome")
{
if(version <= chromeVersion)
{
textToDisplay += "\nUPDATE CHROME - VERSION BELOW " + chromeVersion;
}
else
{
textToDisplay += "\nVERSION GREATER THAN " + chromeVersion;
}
}
else if(browser == "MSIE")
{
if(version <= ieVersion)
{
textToDisplay += "\nUPDATE CHROME - VERSION BELOW " + ieVersion;
}
else
{
textToDisplay += "\nVERSION GREATER THAN " + ieVersion;
}
}
else if (browser == "Firefox")
{
if(version <= firefoxVersion)
{
textToDisplay += "\nUPDATE CHROME - VERSION BELOW " + firefoxVersion;
}
else
{
textToDisplay += "\nVERSION GREATER THAN " + firefoxVersion;
}
}
else if (browser == "Safari")
{
if(version <= safariVersion)
{
textToDisplay += "\nUPDATE CHROME - VERSION BELOW " + safariVersion;
}
else
{
textToDisplay += "\nVERSION GREATER THAN " + safariVersion;
}
}
}
else
{
textToDisplay = "Browser is NOT IE/FIREFOX/SAFARI/CHROME"
}
displayField.text = textToDisplay;
}
答案 0 :(得分:0)
(MSIE(?!。+ Opera)| Firefox(?!。+ SeaMonkey)|版本(?=。+ Safari)(?!。+ Chrome)| Chrome)(?:| /)([\ d] ] +)
我简化了正则表达式以遵循这些检查https://developer.mozilla.org/en-US/docs/Browser_detection_using_the_user_agent。
我不想使用我希望了解的第三方库,因为我可能需要进行更改。以下代码似乎正确地返回我想要的内容。
(MSIE(?!。+ Opera)| Firefox(?!。+ SeaMonkey)|版本(?=。+ Safari)(?!。+ Chrome)| Chrome)(?:| /)([\ d] ] +)