Created
May 12, 2021 23:32
-
-
Save leviem1/478ba776401a02ec88ac0dd3f34fd169 to your computer and use it in GitHub Desktop.
Pessimistically match a version in a string
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* | |
Pessimistically match a version string (e.g. 1.2.3) in string. | |
Works with https://tc39.es/ecma262/ (2018). | |
Pessimistic versioning: | |
https://web.archive.org/web/20201027204727/https://macwright.com/2016/08/23/optimistic-pessimistic-versioning.html | |
https://web.archive.org/web/20210331060738/https://thoughtbot.com/blog/rubys-pessimistic-operator | |
*/ | |
function pessimisticMatch(string, version) { | |
return new RegExp(`(?<!(\\.|[0-9]))${version}(\\.[0-9]*)*(?!.+)`).test(string); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment