admin管理员组

文章数量:1201640

I want to detect Chrome OS with Javascript, and I'm using navigator.userAgent for this. Now, I'm running Chrome OS, and my navigator userAgent is

Mozilla/5.0 (X11; CrOS armv7l 6680.78.0) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/41.0.2272.102 Safari/537.36

Now. I used Regular Expressions to check for the style of the userAgent, and wrote the code below.

<script>
    if ( navigator.userAgent = /^Mozilla\/\d{1}^.\d{1}^(X11; CrOS i\d{3} \d{1}^.\d{2}\d{3} ^AppleWebKit\/\d{3}^.\d{2} ^(KHTML, like Gecko) Chrome\/ \d{2}^.\d{1}^.\d{3}^.\d{2} ^Safari\/\d{3}^\d{2}/ ){
      console.log(navigator.userAgent);
    } else {
      console.log(navigator.userAgent);
    }
</script>

Now, upon loading this Script, I get an error.

Uncaught SyntaxError: Invalid regular expression: /^Mozilla\/\d{1}^.\d{1}^(X11; CrOS i\d{3} \d{1}^.\d{2}\d{3} ^AppleWebKit\/\d{3}^.\d{2} ^(KHTML, like Gecko) Chrome\/ \d{2}^.\d{1}^.\d{3}^.\d{2} ^Safari\/\d{3}^\d{2}/: Unterminated group

What is wrong here with my code?

I want to detect Chrome OS with Javascript, and I'm using navigator.userAgent for this. Now, I'm running Chrome OS, and my navigator userAgent is

Mozilla/5.0 (X11; CrOS armv7l 6680.78.0) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/41.0.2272.102 Safari/537.36

Now. I used Regular Expressions to check for the style of the userAgent, and wrote the code below.

<script>
    if ( navigator.userAgent = /^Mozilla\/\d{1}^.\d{1}^(X11; CrOS i\d{3} \d{1}^.\d{2}\d{3} ^AppleWebKit\/\d{3}^.\d{2} ^(KHTML, like Gecko) Chrome\/ \d{2}^.\d{1}^.\d{3}^.\d{2} ^Safari\/\d{3}^\d{2}/ ){
      console.log(navigator.userAgent);
    } else {
      console.log(navigator.userAgent);
    }
</script>

Now, upon loading this Script, I get an error.

Uncaught SyntaxError: Invalid regular expression: /^Mozilla\/\d{1}^.\d{1}^(X11; CrOS i\d{3} \d{1}^.\d{2}\d{3} ^AppleWebKit\/\d{3}^.\d{2} ^(KHTML, like Gecko) Chrome\/ \d{2}^.\d{1}^.\d{3}^.\d{2} ^Safari\/\d{3}^\d{2}/: Unterminated group

What is wrong here with my code?

Share Improve this question asked Apr 15, 2015 at 17:44 FoxInFlameFoxInFlame 95012 silver badges21 bronze badges 6
  • 3 Before you write more code that looks at the user agent, you might want to consider using feature detection instead. We, of course, could only help you with feature detection if you described the actual problem you're trying to solve with Chrome OS. – jfriend00 Commented Apr 15, 2015 at 17:47
  • @jfriend00 Thank you for the advise, of using feature detection, and I'm sure I'll be using that link later on, but for this web application, I would rather look at the user agent. – FoxInFlame Commented Apr 15, 2015 at 18:03
  • Why do you think user agent detection is better? – jfriend00 Commented Apr 15, 2015 at 18:09
  • @jfriend00 I think so because Chrome OS is essentially just the Chrome Browser. I want to detect the OS, and not the browser type. Correct me if I'm wrong. – FoxInFlame Commented Apr 15, 2015 at 19:09
  • 1 Until you tell us why you're trying to detect Chrome OS (e.g. what the real problem is here), we can't really have a meaningful conversation about using feature detection instead. You're asking about a particular solution you've selected rather than telling us the real problem you're trying to solve which is a common way people limit themselves here on SO. – jfriend00 Commented Apr 15, 2015 at 19:17
 |  Show 1 more comment

3 Answers 3

Reset to default 14

What it's complaining about is that you have an ( with no matching ). In a regular expression, ( and ) define capture groups and have to be balanced. If you want to match an actual ( or ), you have to escape it with a backslash.

But there are several other issues. It doesn't make sense to have ^ ("beginning of input") anywhere but the beginning of the expression, for instance.

But I don't think anything else puts CrOS in the user agent, so perhaps simply:

if (/\bCrOS\b/.test(navigator.userAgent)) {
    // yes, it is (probably, if no one's mucked about with their user agent string)
} else {
    // No, it isn't (probably, if no one's mucked about with their user agent string)
}

The \b are "word boundaries" so we don't match that string in the middle of a word. Note that I left it case-sensitive.


Side note: I find https://regex101.com/#javascript (which I am not in any way affiliated with) quite useful for debugging regular expressions.

Side note #2: The above is useful if you really do need to detect ChromeOS, but if it's just a feature you need to check for, as jfriend00 points out, feature detection may be the better way to go.

How about this?

var chromeOS = /(CrOS)/.test(navigator.userAgent);

Because a Chrome OS user agent looks like this:

Mozilla/5.0 (X11; CrOS i686 0.12.433) AppleWebKit/534.30 (KHTML, like Gecko) Chrome/12.0.742.77 Safari/534.30

And filtering out "CrOS" is a good solution.

I Use This:

It's pretty simple and you can avoid regex. You could add more agents if you wanted to.

// Detect if the OS is Windows or Chrome OS

var detectedOS;

if (window.navigator.userAgent.indexOf("Windows") != -1) {detectedOS = "Windows";}
if (window.navigator.userAgent.indexOf("CrOS") != -1) {detectedOS = "Chrome";}

alert("detectedOS: " + detectedOS);

本文标签: user agentDetecting Chrome OS with JavascriptStack Overflow