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
- Data analytics: Track user browser and device distribution
- Compatibility testing: Identify old browsers and provide fallbacks
- Responsive design: Adjust interface based on device type
- Security analysis: Detect spoofed or unusual User-Agents
- SEO optimization: Provide optimized content per device
- Ad targeting: Serve device-specific advertisements
How to Use an Online Tool
Using DevToolkit Pro's User-Agent Parser:
- Paste the complete User-Agent string
- Auto-parse into structured JSON
- View browser, OS, device details
- Flag mobile/desktop/tablet
- 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.
relatedTools
Related Articles
HTTP Status Codes Reference: Quick Lookup Guide
Complete HTTP status code reference. Understand 1xx, 2xx, 3xx, 4xx, 5xx categories, common scenarios, and correct usage in API development.
Cron Expressions Explained: From '* * * * *' to Complex Schedules
Master cron expression syntax with practical examples. Learn the five fields, step values, ranges, and the day-of-month vs day-of-week gotcha.
Online Color Converter: HEX, RGB, HSL Color Format Conversion
Learn how to use an online color converter to switch between HEX, RGB, and HSL formats. Understand color representation fundamentals and best practices for frontend development.