一、间接上代码
// 测试挪动端环境function isMobileNavigator() { let flag = navigator.userAgent.match(/(phone|pad|pod|iPhone|iPod|ios|iPad|Android|Mobile|BlackBerry|IEMobile|MQQBrowser|JUC|Fennec|wOSBrowser|BrowserNG|WebOS|Symbian|Windows Phone)/i) // match的返回值:如果匹配不到,返回null; 否则返回匹配到的 array return flag;}var isMobile = isMobileNavigator()console.log('isMobile', isMobile)if (isMobile) { // 这里持续进行挪动端的操作}
二、看下打印后果
注: match的应用
string.match(regexp)
- regexp 必须
规定要匹配的模式的 RegExp
对象。如果该参数不是 RegExp 对象,则须要首先把它传递给 RegExp 构造函数,将其转换为 RegExp 对象 - 返回值:Array
寄存匹配后果的 数组
。该数组的内容依赖于 regexp 是否具备全局标记 g。 如果没找到匹配后果返回 null
。