-
Notifications
You must be signed in to change notification settings - Fork 701
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Needs example RegExp #32
Comments
I'm using this one for Ruby:
|
Those are both wrong (in that they will "parse" illegal values). The Javascript regex will allow v2.0.0+-build.acebfde1284 and the Ruby regex would allow v2.0.0-build.acebfde1284+build.acebfde1284. In both cases, a true alternation is required to permit either a hyphen or a plus sign, but not both (and either extra bit is optional). |
@JohnPeacock I thought that both were allowed to coexist: "A pre-release version MAY be denoted by appending a dash (...)" |
@JohnPeacock that's exactly why there needs to some canonical regexes on the main page. |
The spec indicates that pre-release and build version may coexist. The following excerpt from paragraph 12
indicates that the a version with just a pre-release is lower in precedence than the same string with a build appended. I don't disagree that a canonical regexp would be helpful, however. |
Here's my attempt at this:
It validates all of the examples listed on the website at this time. EDIT: I agree with @jlfaber, so I removed the option to support the optional "v" prefix. Original:
|
Perhaps I've missed something, but I don't see anything in the spec that says a leading 'v' is permitted. Lots of folks prefix their versions with a v, of course, but if we're trying to document the spec itself, I don't think that should be there. Here's what I've been using. Note that I've included "capture" parens to return the base, pre-release, and build components. /^(\d+\.\d+\.\d+)(-([0-9A-Za-z-]+(\.[0-9A-Za-z-]+)*))?(\+([0-9A-Za-z-]+(\.[0-9A-Za-z-]+)*))?$/ |
@jlfaber All of your parens are capturing groups though so you end up capturing all sorts of things that I assume you don't wish to... You should use non-capturing groups for those you don't wish to be captured:
Given '2.0.0-rc.1+build.123' that will only capture '2.0.0', 'rc.1' & 'build.123'. |
@Omnikron13 Nice, it also accounts for trailing decimal points. I'll be borrowing this! |
@Omnikron13 Good catch on the non-capturing groups. Thanks! |
In order for sorting to work predictably we have to allow
but disallow
UPDATE: Nevermind, I see that
|
Hi @coolaj86 great idea! This is the repository for the spec. Would you mind submitting this as a pull request over at the website repository? https://github.com/mojombo/semver.org/ We need to consider a nice way to present this information. Should it go in the FAQ or perhaps add a "more info" link to the top header to a page that has this and other information such as links to translations. Thanks! |
For convenience the regular expression that parses a valid semver should be made available on the homepage.
JavaScript:
UPDATE: I think I've collected all of the comments and edge cases to produce a regex and test cases that are in accordance with the spec.
See test cases in JavaScript: https://github.com/coolaj86/semver-utils
The text was updated successfully, but these errors were encountered: