JavaScript test() 方法及判断是否为iPhone|iPad|iPod

  1. //js 判断iPhone|iPad|iPod  
  2. if (/(iPhone|iPad|iPod)/i.test(navigator.userAgent)) {  
  3.     alert(navigator.userAgent);  
  4. };  

定义和用法

test() 方法用于检测一个字符串是否匹配某个模式.

语法

RegExpObject.test(string)
参数描述
string必需。要检测的字符串。

返回值

如果字符串 string 中含有与 RegExpObject 匹配的文本,则返回 true,否则返回 false。

说明

调用 RegExp 对象 r 的 test() 方法,并为它传递字符串 s,与这个表示式是等价的:(r.exec(s) != null)。

原文地址:https://www.cnblogs.com/top5/p/2051222.html