2016-11-10 22 views
0

我很好奇/i是:是什麼/我在navigator.userAgent.match

var isMobile = { 
    Android: function() { 
     return navigator.userAgent.match(/Android/i); 
    }, 
    BlackBerry: function() { 
     return navigator.userAgent.match(/BlackBerry/i); 
    }, 
    iOS: function() { 
     return navigator.userAgent.match(/iPhone|iPad|iPod/i); 
    }, 
    Opera: function() { 
     return navigator.userAgent.match(/Opera Mini/i); 
    }, 
    Windows: function() { 
     return navigator.userAgent.match(/IEMobile/i); 
    }, 
    any: function() { 
     return (isMobile.Android() || isMobile.BlackBerry() || isMobile.iOS() || isMobile.Opera() || isMobile.Windows()); 
    }}; 
} 

來源:https://www.sitepoint.com/navigator-useragent-mobiles-including-ipad/

誰能告訴我是什麼/i是什麼呢?我搜索了很多有關navigator.userAgent.Match東西的網站,但沒有解釋什麼/i是,有時它也是/g

+1

不區分大小寫標誌的當前 位置。所以模式匹配* Android *,* androiD *,或* anDrOid *等 –

+0

所以/我的意思是說它對大寫和小寫不敏感,但是我可以用其他字符替換/ i嗎? – Gerwin

+0

不,你不能。這就是設計。 –

回答

1

/heregoesregex/flags是文字在許多語言(包括JavaScript)正則表達式。在最後一個斜槓之後,您可以爲正則表達式指定標誌。 javascript的可用標誌列表包括:

  • g全局搜索。
  • i不區分大小寫的搜索。
  • m多行搜索。
  • Ÿ「粘」搜索即開始匹配目標串
+2

請注意,ES6也支持'/ u'來支持unicode。 –