Skip to content
utility2026-06-262 min read

What Is User-Agent Parsing

User-Agent parsing converts the browser's User-Agent string into structured information (browser, version, operating system, device type, etc.).

User-Agent String Structure

Mozilla/5.0 (iPhone; CPU iPhone OS 16_0 like Mac OS X)
AppleWebKit/605.1.15 (KHTML, like Gecko)
Version/16.0 Mobile/15E148 Safari/604.1
│           │                    │        │
│           │                    │        └─ Browser version
│           │                    └─ Browser name
│           └─ Operating system
└─ Engine version

Parsed Result Example

{
  "browser": {
    "name": "Safari",
    "version": "16.0",
    "engine": "WebKit",
    "engineVersion": "605.1.15"
  },
  "os": {
    "name": "iOS",
    "version": "16.0",
    "platform": "iPhone"
  },
  "device": {
    "type": "mobile",
    "brand": "Apple",
    "model": "iPhone"
  },
  "isMobile": true,
  "isTablet": false,
  "isDesktop": false
}

Why You Need User-Agent Parsing

  1. Data analytics: Track user browser and device distribution
  2. Compatibility testing: Identify old browsers and provide fallbacks
  3. Responsive design: Adjust interface based on device type
  4. Security analysis: Detect spoofed or unusual User-Agents
  5. SEO optimization: Provide optimized content per device
  6. Ad targeting: Serve device-specific advertisements

How to Use an Online Tool

Using DevToolkit Pro's User-Agent Parser:

  1. Paste the complete User-Agent string
  2. Auto-parse into structured JSON
  3. View browser, OS, device details
  4. Flag mobile/desktop/tablet
  5. One-click copy parsed result

Parsing in Code

JavaScript

function parseUserAgent(ua) {
  const result = {
    browser: { name: 'Unknown', version: '' },
    os: { name: 'Unknown', version: '' },
    device: { type: 'desktop' },
  };

  // Detect OS
  if (/iPhone|iPad|iPod/.test(ua)) {
    result.os.name = 'iOS';
    result.os.platform = /iPhone/.test(ua) ? 'iPhone' : 'iPad';
    result.device.type = /iPad/.test(ua) ? 'tablet' : 'mobile';
  } else if (/Windows/.test(ua)) {
    result.os.name = 'Windows';
    const match = ua.match(/Windows NT (\d+\.\d+)/);
    if (match) result.os.version = match[1];
  } else if (/Mac OS X/.test(ua)) {
    result.os.name = 'macOS';
    const match = ua.match(/Mac OS X (\d+[._]\d+)/);
    if (match) result.os.version = match[1].replace('_', '.');
  } else if (/Android/.test(ua)) {
    result.os.name = 'Android';
    result.device.type = /Mobile/.test(ua) ? 'mobile' : 'tablet';
  }

  // Detect browser
  if (/Chrome/.test(ua) && !/Edg/.test(ua)) {
    result.browser.name = 'Chrome';
    const match = ua.match(/Chrome\/(\d+\.\d+)/);
    if (match) result.browser.version = match[1];
  } else if (/Safari/.test(ua) && !/Chrome/.test(ua)) {
    result.browser.name = 'Safari';
  } else if (/Firefox/.test(ua)) {
    result.browser.name = 'Firefox';
  } else if (/Edg/.test(ua)) {
    result.browser.name = 'Edge';
  }

  return result;
}

Python

import re

def parse_user_agent(ua):
    result = {
        'browser': {'name': 'Unknown', 'version': ''},
        'os': {'name': 'Unknown', 'version': ''},
        'device': {'type': 'desktop'}
    }

    if 'iPhone' in ua or 'iPad' in ua:
        result['os']['name'] = 'iOS'
        result['device']['type'] = 'mobile' if 'iPhone' in ua else 'tablet'
    elif 'Windows' in ua:
        result['os']['name'] = 'Windows'
    elif 'Mac OS X' in ua:
        result['os']['name'] = 'macOS'
    elif 'Android' in ua:
        result['os']['name'] = 'Android'
        result['device']['type'] = 'mobile' if 'Mobile' in ua else 'tablet'

    if 'Chrome' in ua and 'Edg' not in ua:
        result['browser']['name'] = 'Chrome'
    elif 'Safari' in ua and 'Chrome' not in ua:
        result['browser']['name'] = 'Safari'
    elif 'Firefox' in ua:
        result['browser']['name'] = 'Firefox'

    return result

FAQ

Will User-Agent Strings Be Deprecated?

Yes, Google Chrome is rolling out User-Agent Reduction. Future versions will send simplified UA info. Consider using the Client Hints API as a replacement.

How to Detect Bots and Crawlers?

Check for bot, crawler, spider keywords in UA. But note: malicious crawlers can spoof UA, so combine with other signals like request frequency, behavior patterns, and parsing URL parameters for full request context.

How to Distinguish Mobile vs Desktop?

Primary signals: 1) UA contains Mobile/Android/iPhone; 2) Screen resolution (< 768px typically mobile); 3) Touch support (ontouchstart).


This article is brought to you by DevToolkit Pro. More developer tools at the homepage.


Advertisement