Skip to content

Commit

Permalink
Improve regex
Browse files Browse the repository at this point in the history
  • Loading branch information
sindresorhus committed Jun 8, 2022
1 parent 1d451a6 commit b4ff333
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 1 deletion.
2 changes: 1 addition & 1 deletion index.js
@@ -1,3 +1,3 @@
export default function semverRegex() {
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;
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;
}
8 changes: 8 additions & 0 deletions test.js
Expand Up @@ -226,4 +226,12 @@ test('invalid version does not cause catatrophic backtracking', t => {
const difference = Date.now() - start;
t.true(difference < 50, `Execution time: ${difference}`);
}

for (let index = 1; index <= 30; index++) {
const start = Date.now();
const fixture = `0.0.1--${'--'.repeat(index)}\u0000`;
semverRegex().test(fixture);
const difference = Date.now() - start;
t.true(difference < 50, `Execution time: ${difference}`);
}
});

0 comments on commit b4ff333

Please sign in to comment.