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. Combine with other signals (request frequency, behavior patterns).
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.
相关文章
Online Word Counter: Count Words, Characters, Lines Instantly
Learn how to use an online word counter to count words, characters, lines, and paragraphs. Understand Chinese vs English counting differences and use cases in writing and development.
UUID Generator: Complete Guide to Unique Identifiers (UUID, NanoID, ULID)
Understand UUID v4, NanoID, and ULID fundamentals. Learn how to use an online UUID generator, and discover best practices for databases, distributed systems, and API tracking.
Online URL Params Parser: Extract and Analyze URL Query Parameters
Learn how to parse and analyze URL query parameters. Understand URL structure, query string composition, and how to use an online tool to quickly extract and analyze URL parameters.