コンテンツへスキップ
uencoder

User-Agent をパースする方法

User-Agent 文字列からブラウザ、OS、デバイス情報を抽出します。

问题

User-Agent 文字列があり、ブラウザ、OS、デバイスの種類を特定する必要があります。

前提条件

  • HTTP ヘッダーの理解
  • User-Agent の形式に関する知識

ステップ

  1. 1. Understand the User-Agent format

    User-Agent strings contain product tokens separated by spaces, each with a name and optional version. The order and content vary by browser. Modern browsers include compatibility tokens (like Mozilla/5.0) for historical reasons, making parsing complex.

    入力
    Mozilla/5.0 (Windows NT 10.0; Win64; x64) Chrome/118.0
    输出
    tokens identified
  2. 2. Parse with a UA parser library

    Use a dedicated User-Agent parser rather than regex. UA strings are notoriously inconsistent and full of compatibility tokens. A maintained parser library (like ua-parser) uses pattern databases updated for new browsers and devices.

    入力
    UA string
    输出
    browser: Chrome 118, OS: Windows 10, device: desktop
  3. 3. Use the results appropriately

    Use parsed data for analytics, compatibility decisions, or content negotiation. Be aware that User-Agents can be spoofed. Do not make security decisions based solely on UA parsing. Consider Client Hints as a more structured alternative.

    入力
    parsed UA
    输出
    used for analytics

期待される結果

User-Agent 文字列から抽出されたブラウザ、OS、デバイスの情報。

よくあるエラー

Parsing with regex misses new browsers
Use a maintained UA parser library with an updated pattern database. Regex-based parsing breaks on new UA formats.
Trusting UA for security decisions
User-Agents are client-controlled and can be spoofed. Never use UA alone for authentication or authorization.

トラブルシューティング指引

  • If parsing returns 'unknown', the UA may be from a bot, app, or new browser. Update the parser database.
  • Client Hints (Sec-CH-UA) provide structured browser data and are more reliable than UA strings.

セキュリティ上の制限

  • User-Agent strings are spoofable. Never rely on them for security. Use feature detection instead of UA sniffing for browser compatibility.

関連ツール

参考链接