Regular Expressions 101

Community Patterns

POC Mobile Detection

0

Regular Expression
Golang

`
(?:Mobile|Android|webOS|iPhone|iPad|iPod|BlackBerry|IEMobile|Opera\sMobi)
`
gis

Description

const regex = /(?:Mobile|Android|webOS|iPhone|iPad|iPod|BlackBerry|IEMobile|Opera\sMobi)/gm; const str = `Mozilla/5.0 (Android; Mobile; rv:13.0) Gecko/13.0 Firefox/13.0

Mozilla/5.0 (Linux; U; Android 4.0.3; de-ch; HTC Sensation Build/IML74K) AppleWebKit/534.30 (KHTML, like Gecko) Version/4.0 Mobile Safari/534.30

Mozilla/5.0 (Linux; Android 4.4.2); Nexus 5 Build/KOT49H) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/33.0.1750.117 Mobile Safari/537.36 OPR/20.0.1396.72047

Opera/9.80 (Android 2.3.3; Linux; Opera Mobi/ADR-1111101157; U; es-ES) Presto/2.9.201 Version/11.50

Mozilla/5.0 (compatible; MSIE 9.0; Windows Phone OS 7.5; Trident/5.0; IEMobile/9.0)

Mozilla/5.0 (Windows Phone 10.0; Android 6.0.1; Xbox; Xbox One) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/58.0.3029.110 Mobile Safari/537.36 Edge/16.16299`; let m;

while ((m = regex.exec(str)) !== null) { // This is necessary to avoid infinite loops with zero-width matches if (m.index === regex.lastIndex) { regex.lastIndex++; }

// The result can be accessed through the `m`-variable.
m.forEach((match, groupIndex) => {
    console.log(`Found match, group ${groupIndex}: ${match}`);
});

}

Submitted by anonymous - 3 years ago