{"id":13448518,"url":"https://github.com/browserslist/browserslist-useragent-regexp","last_synced_at":"2025-05-14T08:05:21.193Z","repository":{"id":38273368,"uuid":"176157005","full_name":"browserslist/browserslist-useragent-regexp","owner":"browserslist","description":"A utility to compile browserslist query to a RegExp to test browser useragent.","archived":false,"fork":false,"pushed_at":"2025-03-15T17:50:46.000Z","size":3337,"stargazers_count":256,"open_issues_count":19,"forks_count":19,"subscribers_count":5,"default_branch":"master","last_synced_at":"2025-05-02T01:31:49.992Z","etag":null,"topics":[],"latest_commit_sha":null,"homepage":"","language":"TypeScript","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/browserslist.png","metadata":{"files":{"readme":"README.md","changelog":"CHANGELOG.md","contributing":null,"funding":".github/FUNDING.yml","license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null,"zenodo":null},"funding":{"open_collective":"browserslist-useragent-regexp","ko_fi":"dangreen"}},"created_at":"2019-03-17T20:30:41.000Z","updated_at":"2025-04-08T20:43:24.000Z","dependencies_parsed_at":"2023-11-07T17:03:33.593Z","dependency_job_id":"f2d66e6a-02c1-4a15-ac5d-aca757083ced","html_url":"https://github.com/browserslist/browserslist-useragent-regexp","commit_stats":{"total_commits":488,"total_committers":9,"mean_commits":54.22222222222222,"dds":0.5860655737704918,"last_synced_commit":"7d33c79922fe6d935a1e752a44e9dbd83a81e69a"},"previous_names":[],"tags_count":21,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/browserslist%2Fbrowserslist-useragent-regexp","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/browserslist%2Fbrowserslist-useragent-regexp/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/browserslist%2Fbrowserslist-useragent-regexp/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/browserslist%2Fbrowserslist-useragent-regexp/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/browserslist","download_url":"https://codeload.github.com/browserslist/browserslist-useragent-regexp/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":252675370,"owners_count":21786825,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":[],"created_at":"2024-07-31T05:01:47.868Z","updated_at":"2025-05-14T08:05:21.177Z","avatar_url":"https://github.com/browserslist.png","language":"TypeScript","funding_links":["https://opencollective.com/browserslist-useragent-regexp","https://ko-fi.com/dangreen"],"categories":["TypeScript"],"sub_categories":[],"readme":"# browserslist-useragent-regexp\n\n[![ESM-only package][package]][package-url]\n[![NPM version][npm]][npm-url]\n[![Node version][node]][node-url]\n[![Dependencies status][deps]][deps-url]\n[![Install size][size]][size-url]\n[![Build status][build]][build-url]\n[![Coverage status][coverage]][coverage-url]\n[![Documentation badge][documentation]][documentation-url]\n\n[package]: https://img.shields.io/badge/package-ESM--only-ffe536.svg\n[package-url]: https://nodejs.org/api/esm.html\n\n[npm]: https://img.shields.io/npm/v/browserslist-useragent-regexp.svg\n[npm-url]: https://npmjs.com/package/browserslist-useragent-regexp\n\n[node]: https://img.shields.io/node/v/browserslist-useragent-regexp.svg\n[node-url]: https://nodejs.org\n\n[deps]: https://img.shields.io/librariesio/release/npm/browserslist-useragent-regexp\n[deps-url]: https://libraries.io/npm/browserslist-useragent-regexp/tree\n\n[size]: https://packagephobia.com/badge?p=browserslist-useragent-regexp\n[size-url]: https://packagephobia.com/result?p=browserslist-useragent-regexp\n\n[build]: https://img.shields.io/github/actions/workflow/status/browserslist/browserslist-useragent-regexp/tests.yml?branch=master\n[build-url]: https://github.com/browserslist/browserslist-useragent-regexp/actions\n\n[coverage]: https://img.shields.io/codecov/c/github/browserslist/browserslist-useragent-regexp.svg\n[coverage-url]: https://app.codecov.io/gh/browserslist/browserslist-useragent-regexp\n\n[documentation]: https://img.shields.io/badge/API-Documentation-2b7489.svg\n[documentation-url]: https://browserslist.github.io/browserslist-useragent-regexp\n\nA utility to compile [browserslist query](https://github.com/browserslist/browserslist#queries) to a regex to test browser useragent. Simplest example: you can detect supported browsers on client-side.\n\n1) Create `.browserslistrc` config, for example like this:\n\n```\nlast 2 versions\nnot dead\n```\n\n2) Add script to `package.json`:\n\n```json\n{\n  \"scripts\": {\n    \"supportedBrowsers\": \"echo \\\"export default $(browserslist-useragent-regexp --allowHigherVersions);\\\" \u003e supportedBrowsers.js\"\n  }\n}\n```\n\n\u003cdetails\u003e\n  \u003csummary\u003efor Windows users\u003c/summary\u003e\n\n```json\n{\n  \"scripts\": {\n    \"supportedBrowsers\": \"(echo export default \u0026\u0026 browserslist-useragent-regexp --allowHigherVersions) \u003e supportedBrowsers.js\"\n  }\n}\n```\n\n\u003c/details\u003e\n\n3) Run this script, to compile regex:\n\n```bash\npnpm supportedBrowsers\n# or\nnpm run supportedBrowsers\n# or\nyarn supportedBrowsers\n```\n\n`supportedBrowsers.js`:\n\n```js\nexport default /Edge?\\/(10[5-9]|1[1-9]\\d|[2-9]\\d\\d|\\d{4,})(\\.\\d+|)(\\.\\d+|)|Firefox\\/(10[4-9]|1[1-9]\\d|[2-9]\\d\\d|\\d{4,})\\.\\d+(\\.\\d+|)|Chrom(ium|e)\\/(10[5-9]|1[1-9]\\d|[2-9]\\d\\d|\\d{4,})\\.\\d+(\\.\\d+|)|Maci.* Version\\/(15\\.([6-9]|\\d{2,})|(1[6-9]|[2-9]\\d|\\d{3,})\\.\\d+)([,.]\\d+|)( Mobile\\/\\w+|) Safari\\/|Chrome.+OPR\\/(9\\d|\\d{3,})\\.\\d+\\.\\d+|(CPU[ +]OS|iPhone[ +]OS|CPU[ +]iPhone|CPU IPhone OS|CPU iPad OS)[ +]+(15[._]([6-9]|\\d{2,})|(1[6-9]|[2-9]\\d|\\d{3,})[._]\\d+)([._]\\d+|)|Opera Mini|Android:?[ /\\-](10[6-9]|1[1-9]\\d|[2-9]\\d{2}|\\d{4,})(\\.\\d+|)(\\.\\d+|)|Mobile Safari.+OPR\\/(6[4-9]|[7-9]\\d|\\d{3,})\\.\\d+\\.\\d+|Android.+Firefox\\/(10[5-9]|1[1-9]\\d|[2-9]\\d\\d|\\d{4,})\\.\\d+(\\.\\d+|)|Android.+Chrom(ium|e)\\/(10[6-9]|1[1-9]\\d|[2-9]\\d\\d|\\d{4,})\\.\\d+(\\.\\d+|)|Android.+(UC? ?Browser|UCWEB|U3)[ /]?(13\\.([4-9]|\\d{2,})|(1[4-9]|[2-9]\\d|\\d{3,})\\.\\d+)\\.\\d+|SamsungBrowser\\/(1[7-9]|[2-9]\\d|\\d{3,})\\.\\d+|Android.+MQQBrowser\\/(13(\\.([1-9]|\\d{2,})|)|(1[4-9]|[2-9]\\d|\\d{3,})(\\.\\d+|))(\\.\\d+|)|K[Aa][Ii]OS\\/(2\\.([5-9]|\\d{2,})|([3-9]|\\d{2,})\\.\\d+)(\\.\\d+|)/;\n```\n\n4) Import regex from created file:\n\n```js\nimport supportedBrowsers from './supportedBrowsers.js';\n\nif (supportedBrowsers.test(navigator.userAgent)) {\n  alert('Your browser is supported.');\n}\n```\n\n## Install\n\n```bash\npnpm add -D browserslist-useragent-regexp\n# or\nnpm i -D browserslist-useragent-regexp\n# or\nyarn add -D browserslist-useragent-regexp\n```\n\n## Why?\n\nAs was written in article [\"Smart Bundling: Shipping legacy code to only legacy browsers\"](https://www.smashingmagazine.com/2018/10/smart-bundling-legacy-code-browsers/): you can determinate, which bundle you should give to browser from server with [`browserslist-useragent`](https://github.com/browserslist/browserslist-useragent). But in this case you must have your own server with special logic. Now, with `browserslist-useragent-regexp`, you can move that to client-side.\n\nDevelopment was inspired by [this proposal from Mathias Bynens](https://twitter.com/mathias/status/1105857829393653761).\n\nHow to make differential resource loading and other optimizations with `browserslist-useragent-regexp` you can read in article [\"Speed up with Browserslist\"](https://dev.to/dangreen/speed-up-with-browserslist-30lh).\n\n[Demo](https://browserslist.github.io/browserslist-useragent-regexp/demo.html) ([sources](https://github.com/browserslist/browserslist-useragent-regexp/blob/7cf6afb7da2b6c77179abb8b8bd1bbcb61cf376a/docs/demo.html#L17-L29), [build script](https://github.com/browserslist/browserslist-useragent-regexp/blob/7cf6afb7da2b6c77179abb8b8bd1bbcb61cf376a/examples/buildDemo.js#L61-L74)).\n\nAlso, testing useragents using generated regex [is faster](https://gist.github.com/dangreen/55c41072d8891efd3a772a4739d6cd9d) than using the `matchesUA` method from browserslist-useragent.\n\n## CLI\n\n```bash\npnpm browserslist-useragent-regexp [query] [...options]\n# or\nnpx browserslist-useragent-regexp [query] [...options]\n# or\nyarn exec -- browserslist-useragent-regexp [query] [...options]\n```\n\nAlso, short alias is available:\n\n```bash\npnpm bluare [query] [...options]\n```\n\n| Option | Description | Default |\n|--------|-------------|---------|\n| query | Manually provide a browserslist query. Specifying this overrides the browserslist configuration specified in your project. | |\n| \u0026#x2011;\u0026#x2011;help, -h | Print this message. | |\n| \u0026#x2011;\u0026#x2011;verbose, -v | Print additional info about regexes. | |\n| \u0026#x2011;\u0026#x2011;ignorePatch | Ignore differences in patch browser numbers. | `true` |\n| \u0026#x2011;\u0026#x2011;ignoreMinor | Ignore differences in minor browser versions. | `false` |\n| \u0026#x2011;\u0026#x2011;allowHigherVersions | For all the browsers in the browserslist query, return a match if the useragent version is equal to or higher than the one specified in browserslist. | `false` |\n| \u0026#x2011;\u0026#x2011;allowZeroSubversions | Ignore match of patch or patch and minor, if they are 0. | `false` |\n\n## JS API basics\n\nModule exposes two main methods:\n\n### [getUserAgentRegexes(options)](https://browserslist.github.io/browserslist-useragent-regexp/functions/getUserAgentRegexes.html)\n\nCompile browserslist query to [regexes for each browser](#regex-info-object).\n\n### [getUserAgentRegex(options)](https://browserslist.github.io/browserslist-useragent-regexp/functions/getUserAgentRegex.html)\n\nCompile browserslist query to one regex.\n\n\u003e [Description of all methods you can find in Documentation.](https://browserslist.github.io/browserslist-useragent-regexp/index.html)\n\n#### Options\n\n| Option | Type | Default | Description |\n|--------|------|---------|-------------|\n| browsers | `string \\| string[]` | — | Manually provide a browserslist query (or an array of queries). Specifying this overrides the browserslist configuration specified in your project. |\n| ignorePatch | `boolean` | `true` | Ignore differences in patch browser numbers. |\n| ignoreMinor | `boolean` | `false` | Ignore differences in minor browser versions. |\n| allowHigherVersions | `boolean` | `false` | For all the browsers in the browserslist query, return a match if the useragent version is equal to or higher than the one specified in browserslist. |\n| allowZeroSubversions | `boolean` | `false` | Ignore match of patch or patch and minor, if they are 0. |\n\nAny of the [`browserslist` API options](https://github.com/browserslist/browserslist#js-api) may also be provided.\n\n#### Regex info object\n\n| Property | Type | Description |\n|----------|------|-------------|\n| family | `string` | Browser family. |\n| requestVersions | `[number, number, number][]` | Versions provided by browserslist. |\n| regex | `RegExp` | Regex to match useragent with family and versions. |\n| sourceRegex | `RegExp` | Original useragent regex, without versions. |\n| version | `[number, number, number] \\| null` | Useragent version of regex. |\n| minVersion | `[number, number, number] \\| null` | Useragent min version of regex. |\n| maxVersion | `[number, number, number] \\| null` | Useragent max version of regex. |\n\n## Other\n\n- [Supported browsers](https://github.com/browserslist/browserslist-useragent#supported-browsers)\n- [Notes](https://github.com/browserslist/browserslist-useragent#notes)\n- [When querying for modern browsers](https://github.com/browserslist/browserslist-useragent#when-querying-for-modern-browsers)\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fbrowserslist%2Fbrowserslist-useragent-regexp","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fbrowserslist%2Fbrowserslist-useragent-regexp","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fbrowserslist%2Fbrowserslist-useragent-regexp/lists"}