Skip to content

Instantly share code, notes, and snippets.

@leviem1
Created May 12, 2021 23:32
Show Gist options
  • Save leviem1/478ba776401a02ec88ac0dd3f34fd169 to your computer and use it in GitHub Desktop.
Save leviem1/478ba776401a02ec88ac0dd3f34fd169 to your computer and use it in GitHub Desktop.
Pessimistically match a version in a string
/*
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