├── .editorconfig ├── .gitattributes ├── .github ├── funding.yml ├── security.md └── workflows │ └── main.yml ├── .gitignore ├── .npmrc ├── index.d.ts ├── index.js ├── index.test-d.ts ├── license ├── package.json ├── readme.md └── test.js /.editorconfig: -------------------------------------------------------------------------------- 1 | root = true 2 | 3 | [*] 4 | indent_style = tab 5 | end_of_line = lf 6 | charset = utf-8 7 | trim_trailing_whitespace = true 8 | insert_final_newline = true 9 | 10 | [*.yml] 11 | indent_style = space 12 | indent_size = 2 13 | -------------------------------------------------------------------------------- /.gitattributes: -------------------------------------------------------------------------------- 1 | * text=auto eol=lf 2 | -------------------------------------------------------------------------------- /.github/funding.yml: -------------------------------------------------------------------------------- 1 | github: sindresorhus 2 | open_collective: sindresorhus 3 | tidelift: npm/semver-regex 4 | custom: https://sindresorhus.com/donate 5 | -------------------------------------------------------------------------------- /.github/security.md: -------------------------------------------------------------------------------- 1 | # Security Policy 2 | 3 | To report a security vulnerability, please use the [Tidelift security contact](https://tidelift.com/security). Tidelift will coordinate the fix and disclosure. 4 | -------------------------------------------------------------------------------- /.github/workflows/main.yml: -------------------------------------------------------------------------------- 1 | name: CI 2 | on: 3 | - push 4 | - pull_request 5 | jobs: 6 | test: 7 | name: Node.js ${{ matrix.node-version }} 8 | runs-on: ubuntu-latest 9 | strategy: 10 | fail-fast: false 11 | matrix: 12 | node-version: 13 | - 18 14 | - 16 15 | - 14 16 | steps: 17 | - uses: actions/checkout@v3 18 | - uses: actions/setup-node@v3 19 | with: 20 | node-version: ${{ matrix.node-version }} 21 | - run: npm install 22 | - run: npm test 23 | -------------------------------------------------------------------------------- /.gitignore: -------------------------------------------------------------------------------- 1 | node_modules 2 | yarn.lock 3 | -------------------------------------------------------------------------------- /.npmrc: -------------------------------------------------------------------------------- 1 | package-lock=false 2 | -------------------------------------------------------------------------------- /index.d.ts: -------------------------------------------------------------------------------- 1 | /** 2 | Regular expression for matching [semver](https://github.com/npm/node-semver) versions. 3 | 4 | @example 5 | ``` 6 | import semverRegex from 'semver-regex'; 7 | 8 | semverRegex().test('v1.0.0'); 9 | //=> true 10 | 11 | semverRegex().test('1.2.3-alpha.10.beta.0+build.unicorn.rainbow'); 12 | //=> true 13 | 14 | semverRegex().exec('unicorn 1.0.0 rainbow')[0]; 15 | //=> '1.0.0' 16 | 17 | 'unicorn 1.0.0 and rainbow 2.1.3'.match(semverRegex()); 18 | //=> ['1.0.0', '2.1.3'] 19 | ``` 20 | */ 21 | export default function semverRegex(): RegExp; 22 | -------------------------------------------------------------------------------- /index.js: -------------------------------------------------------------------------------- 1 | export default function semverRegex() { 2 | return /(?<=^v?|\sv?)(?:(?:0|[1-9]\d{0,9}?)\.){2}(?:0|[1-9]\d{0,9})(?:-(?:--+)?(?:0|[1-9]\d*|\d*[a-z]+\d*)){0,100}(?=$| |\+|\.)(?:(?<=-\S+)(?:\.(?:--?|[\da-z-]*[a-z-]\d*|0|[1-9]\d*)){1,100}?)?(?!\.)(?:\+(?:[\da-z]\.?-?){1,100}?(?!\w))?(?!\+)/gi; 3 | } 4 | -------------------------------------------------------------------------------- /index.test-d.ts: -------------------------------------------------------------------------------- 1 | import {expectType} from 'tsd'; 2 | import semverRegex from './index.js'; 3 | 4 | expectType(semverRegex()); 5 | -------------------------------------------------------------------------------- /license: -------------------------------------------------------------------------------- 1 | MIT License 2 | 3 | Copyright (c) Sindre Sorhus (https://sindresorhus.com) 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: 6 | 7 | The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. 8 | 9 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. 10 | -------------------------------------------------------------------------------- /package.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "semver-regex", 3 | "version": "4.0.5", 4 | "description": "Regular expression for matching semver versions", 5 | "license": "MIT", 6 | "repository": "sindresorhus/semver-regex", 7 | "funding": "https://github.com/sponsors/sindresorhus", 8 | "author": { 9 | "name": "Sindre Sorhus", 10 | "email": "sindresorhus@gmail.com", 11 | "url": "https://sindresorhus.com" 12 | }, 13 | "type": "module", 14 | "exports": "./index.js", 15 | "engines": { 16 | "node": ">=12" 17 | }, 18 | "scripts": { 19 | "test": "xo && ava && tsd" 20 | }, 21 | "files": [ 22 | "index.js", 23 | "index.d.ts" 24 | ], 25 | "keywords": [ 26 | "semver", 27 | "version", 28 | "versions", 29 | "regex", 30 | "regexp", 31 | "match", 32 | "matching", 33 | "semantic" 34 | ], 35 | "devDependencies": { 36 | "ava": "^4.3.0", 37 | "tsd": "^0.20.0", 38 | "xo": "^0.49.0", 39 | "semver": "^7.3.7" 40 | } 41 | } 42 | -------------------------------------------------------------------------------- /readme.md: -------------------------------------------------------------------------------- 1 | # semver-regex 2 | 3 | > Regular expression for matching [semver](https://github.com/npm/node-semver) versions 4 | 5 | ## Install 6 | 7 | ```sh 8 | npm install semver-regex 9 | ``` 10 | 11 | ## Usage 12 | 13 | ```js 14 | import semverRegex from 'semver-regex'; 15 | 16 | semverRegex().test('v1.0.0'); 17 | //=> true 18 | 19 | semverRegex().test('1.2.3-alpha.10.beta.0+build.unicorn.rainbow'); 20 | //=> true 21 | 22 | semverRegex().exec('unicorn 1.0.0 rainbow')[0]; 23 | //=> '1.0.0' 24 | 25 | 'unicorn 1.0.0 and rainbow 2.1.3'.match(semverRegex()); 26 | //=> ['1.0.0', '2.1.3'] 27 | ``` 28 | 29 | ## Important 30 | 31 | If you run the regex against untrusted user input, it's recommended to truncate the string to a sensible length (for example, 50). And if you use this in a server context, you should also [give it a timeout](https://github.com/sindresorhus/super-regex). 32 | 33 | **I do not consider ReDoS a valid vulnerability for this package. It's simply not possible to make it fully ReDoS safe. It's up to the user to set a timeout for the regex if they accept untrusted user input.** However, I'm happy to accept pull requests to improve the regex. 34 | 35 | ## Related 36 | 37 | - [find-versions](https://github.com/sindresorhus/find-versions) - Find semver versions in a string 38 | - [latest-semver](https://github.com/sindresorhus/latest-semver) - Get the latest stable semver version from an array of versions 39 | - [to-semver](https://github.com/sindresorhus/to-semver) - Get an array of valid, sorted, and cleaned semver versions from an array of strings 40 | - [semver-diff](https://github.com/sindresorhus/semver-diff) - Get the diff type of two semver versions: `0.0.1` `0.0.2` → `patch` 41 | - [semver-truncate](https://github.com/sindresorhus/semver-truncate) - Truncate a semver version: `1.2.3` → `1.2.0` 42 | -------------------------------------------------------------------------------- /test.js: -------------------------------------------------------------------------------- 1 | import test from 'ava'; 2 | import semver from 'semver'; 3 | import semverRegex from './index.js'; 4 | 5 | const validStrings = [ 6 | '0.0.0', 7 | '0.10.0', 8 | 'v1.0.0', 9 | '0.0.0-foo', 10 | '0.0.0-foo-bar-baz', 11 | '1.2.3-4', 12 | '2.7.2+asdf', 13 | '1.2.3-a.b.c.10.d.5', 14 | '2.7.2-foo+bar', 15 | '1.2.3-alpha.10.beta', 16 | '1.2.3-alpha.10.beta+build.unicorn.rainbow', 17 | '99999.99999.99999', 18 | 19 | // Pulled from https://regex101.com/r/vkijKf/1/ 20 | '0.0.4', 21 | '1.2.3', 22 | '10.20.30', 23 | '1.1.2-prerelease+meta', 24 | '1.1.2+meta', 25 | '1.1.2+meta-valid', 26 | '1.0.0-alpha', 27 | '1.0.0-beta', 28 | '1.0.0-alpha.beta', 29 | '1.0.0-alpha.beta.1', 30 | '1.0.0-alpha.1', 31 | '1.0.0-alpha0.valid', 32 | '1.0.0-alpha.va1id', 33 | '1.0.0-alpha.0valid', 34 | '1.0.0-alpha-a.b-c-somethinglong+build.1-aef.1-its-okay', 35 | '1.0.0-rc.1+build.1', 36 | '2.0.0-rc.1+build.123', 37 | '1.2.3-beta', 38 | '10.2.3-DEV-SNAPSHOT', 39 | '1.2.3-SNAPSHOT-123', 40 | '1.0.0', 41 | '2.0.0', 42 | '1.1.7', 43 | '2.0.0+build.1848', 44 | '2.0.1-alpha.1227', 45 | '1.0.0-alpha+beta', 46 | '1.2.3----RC-SNAPSHOT.12.9.1--.12+788', 47 | '1.2.3----R-S.12.9.1--.12+meta', 48 | '1.2.3----RC-SNAPSHOT.12.9.1--.12', 49 | '1.0.0+0.build.1-rc.10000aaa-kk-0.1', 50 | // '99999999999999999999999.999999999999999999.99999999999999999', // Too long 51 | '1.0.0-0A.is.legal', 52 | ]; 53 | 54 | const invalidStrings = [ 55 | '1', 56 | '1.2', 57 | '1.2.3-0123', 58 | '1.2.3-0123.0123', 59 | '1.1.2+.123', 60 | '+invalid', 61 | '-invalid', 62 | '-invalid+invalid', 63 | '-invalid.01', 64 | 'alpha', 65 | 'alpha.beta', 66 | 'alpha.beta.1', 67 | 'alpha.1', 68 | 'alpha+beta', 69 | 'alpha_beta', 70 | 'alpha.', 71 | 'alpha..', 72 | 'beta', 73 | '1.0.0-alpha_beta', 74 | '-alpha.', 75 | '1.0.0-alpha..', 76 | '1.0.0-alpha..1', 77 | '1.0.0-alpha...1', 78 | '1.0.0-alpha....1', 79 | '1.0.0-alpha.....1', 80 | '1.0.0-alpha......1', 81 | '1.0.0-alpha.......1', 82 | '01.1.1', 83 | '1.01.1', 84 | '1.1.01', 85 | '1.2', 86 | '1.2.3.DEV', 87 | '1.2-SNAPSHOT', 88 | '1.2.31.2.3----RC-SNAPSHOT.12.09.1--..12+788', 89 | '1.2-RC-SNAPSHOT', 90 | '-1.0.3-gamma+b7718', 91 | '+justmeta', 92 | '9.8.7+meta+meta', 93 | '9.8.7-whatever+meta+meta', 94 | '99999999999999999999999.999999999999999999.99999999999999999----RC-SNAPSHOT.12.09.1--------------------------------..12', 95 | '1.0.0-beta@beta', 96 | ]; 97 | 98 | test('matches semver versions on test', t => { 99 | for (const fixture of validStrings) { 100 | t.regex(fixture, semverRegex()); 101 | t.true(semver.valid(fixture) !== null); 102 | 103 | if (!fixture.startsWith('v')) { // Should we trim v prefix? 104 | t.deepEqual(fixture.match(semverRegex()), [fixture]); 105 | } 106 | } 107 | 108 | t.notRegex('0.88', semverRegex()); 109 | t.notRegex('1.0.08', semverRegex()); 110 | t.notRegex('1.08.0', semverRegex()); 111 | t.notRegex('01.8.0', semverRegex()); 112 | }); 113 | 114 | test('returns semver on match', t => { 115 | t.deepEqual('0.0.0'.match(semverRegex()), ['0.0.0']); 116 | t.deepEqual('foo 0.0.0 bar 0.1.1'.match(semverRegex()), ['0.0.0', '0.1.1']); 117 | t.deepEqual('1.2.3-alpha.10.beta'.match(semverRegex()), ['1.2.3-alpha.10.beta']); 118 | t.deepEqual('0.0.0-foo-bar alpha.beta.1 1.2.31.2.3----RC-SNAPSHOT.12.09.1--..12+788 1.0.0-alpha+beta 1.2.3----RC-SNAPSHOT.12.9.1--.12+788 1.2 1.2.3-4'.match(semverRegex()), ['0.0.0-foo-bar', '1.0.0-alpha+beta', '1.2.3----RC-SNAPSHOT.12.9.1--.12+788', '1.2.3-4']); 119 | }); 120 | 121 | test('#7, does not return tag prefix', t => { 122 | t.deepEqual('v0.0.0'.match(semverRegex()), ['0.0.0']); 123 | }); 124 | 125 | test('#14, does not match sub-strings of longer semver-similar strings, respect semver@2.0.0 clause 9', t => { 126 | for (const string of invalidStrings) { 127 | t.notRegex(string, semverRegex()); 128 | t.true(semver.valid(string) === null); 129 | } 130 | }); 131 | 132 | test('#18, allow 0 as numeric identifier', t => { 133 | for (const string of [ 134 | '0.2.3-alpha.10.beta+build.unicorn.rainbow', 135 | '1.0.3-alpha.10.beta+build.unicorn.rainbow', 136 | '1.2.0-alpha.10.beta+build.unicorn.rainbow', 137 | '1.2.3-0.10.beta+build.unicorn.rainbow', 138 | '1.2.3-alpha.0.beta+build.unicorn.rainbow', 139 | '1.2.3-alpha.10.0+build.unicorn.rainbow', 140 | ]) { 141 | t.regex(string, semverRegex()); 142 | t.true(semver.valid(string) !== null); 143 | } 144 | }); 145 | 146 | // If tests take longer than a second, it's stuck on this and we have catatrophic backtracking. 147 | test('invalid version does not cause catatrophic backtracking', t => { 148 | t.regex( 149 | 'v1.1.3-0aa.aa.aa.aa.aa.aa.aa.aa.aa.aa.aa.aa.aa.aa.aa.aa.aa.aa.aa.aa.aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa$', 150 | semverRegex(), 151 | ); 152 | 153 | const postfix = '.aa.aa.aa.aa.aa.aa.aa.aa.aa.aa.aa.aa.aa.aa.aa.aa.aa.aa.aa.aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa'.repeat(99_999); 154 | t.regex( 155 | `v1.1.3-0aa${postfix}$`, 156 | semverRegex(), 157 | ); 158 | 159 | for (let index = 1; index <= 50_000; index++) { 160 | const start = Date.now(); 161 | const fixture = `0.0.0-0${'.-------'.repeat(index)}@`; 162 | semverRegex().test(fixture); 163 | const difference = Date.now() - start; 164 | t.true(difference < 20, `Execution time: ${difference}`); 165 | } 166 | 167 | for (let index = 1; index <= 20; index++) { 168 | const start = Date.now(); 169 | const fixture = `0.0.1-${'-.--'.repeat(index)} `; 170 | semverRegex().test(fixture); 171 | const difference = Date.now() - start; 172 | t.true(difference < 20, `Execution time: ${difference}`); 173 | } 174 | 175 | for (let index = 1; index <= 30; index++) { 176 | // Attack string generated by https://devina.io/redos-checker 177 | const start = Date.now(); 178 | const fixtures = [ 179 | '0.0.1-i' + '--i-'.repeat(index) + '\u0000', 180 | '0' + ' 0.1.0-i0'.repeat(index) + '.1.1+1' + '1'.repeat(index) + 'A', 181 | '1.0.1--' + '-'.repeat(index) + '\u0000', 182 | 'g' + ' 0.0.1-i+'.repeat(index) + 'a' + 'v0'.repeat(index) + '\u0000', 183 | ]; 184 | for (const fixture of fixtures) { 185 | semverRegex().test(fixture); 186 | } 187 | 188 | const difference = Date.now() - start; 189 | t.true(difference < 20, `Execution time: ${difference}`); 190 | } 191 | 192 | for (let index = 1; index <= 100; index++) { 193 | const start = Date.now(); 194 | const shuffle = array => array.sort(() => Math.random() - 0.5); 195 | 196 | // Adapted from https://gist.github.com/6174/6062387 197 | const randomString = (() => { 198 | const gen = (min, max) => max++ && Array.from({length: max - min}).map((s, i) => String.fromCodePoint(min + i)); 199 | 200 | const sets = { 201 | num: gen(48, 57), 202 | alphaLower: gen(97, 122), 203 | alphaUpper: gen(65, 90), 204 | special: [...'~!@#$%^&*()_+-=[]{}|;:\'",./<>?'], 205 | }; 206 | 207 | function * iter(length, set) { 208 | if (set.length === 0) { 209 | set = Object.values(sets).flat(); 210 | } 211 | 212 | for (let index = 0; index < length; index++) { 213 | yield set[Math.trunc(Math.random() * set.length)]; 214 | } 215 | } 216 | 217 | return Object.assign(((length, ...set) => [...iter(length, set.flat())].join('')), sets); 218 | })(); 219 | 220 | const fuzz = Array.from({length: 100}).map(() => randomString(100 * Math.random(), randomString.alphaUpper, randomString.special, randomString.alphaLower, randomString.num)); 221 | 222 | const fixture = shuffle(Array.from({length: index}).map(() => [validStrings, invalidStrings, fuzz]).flat(2)).join(' '); 223 | 224 | semverRegex().test(fixture); 225 | 226 | const difference = Date.now() - start; 227 | t.true(difference < 50, `Execution time: ${difference}`); 228 | } 229 | 230 | for (let index = 1; index <= 30; index++) { 231 | const start = Date.now(); 232 | const fixture = `0.0.1--${'--'.repeat(index)}\u0000`; 233 | semverRegex().test(fixture); 234 | const difference = Date.now() - start; 235 | t.true(difference < 50, `Execution time: ${difference}`); 236 | } 237 | }); 238 | --------------------------------------------------------------------------------