Regex for semantic version

/\bv?(?:0|[1-9]\d*)\.(?:0|[1-9]\d*)\.(?:0|[1-9]\d*)(?:-[0-9A-Za-z.-]+)?(?:\+[0-9A-Za-z.-]+)?\b/g

Options
Matches

Examples

Note

Follows semver.org: no leading zeros, optional pre-release after a hyphen and build metadata after a plus.

Matches in the example

  • "1.2.3"
  • "v2.0.0-rc.1+build.5"

Token by token

  1. \b a word boundary
  2. v? the character “v”, optionally
  3. (?: a group, not capturing
  4. 0 the character “0”
  5. | or
  6. [1-9] one of: “1” to “9”
  7. \d* a digit (0–9), zero or more times, as many as possible
  8. ) end of group
  9. \. the character “.”
  10. (?: a group, not capturing
  11. 0 the character “0”
  12. | or
  13. [1-9] one of: “1” to “9”
  14. \d* a digit (0–9), zero or more times, as many as possible
  15. ) end of group
  16. \. the character “.”
  17. (?: a group, not capturing
  18. 0 the character “0”
  19. | or
  20. [1-9] one of: “1” to “9”
  21. \d* a digit (0–9), zero or more times, as many as possible
  22. ) end of group
  23. (?: a group, not capturing
  24. - the character “-”
  25. [0-9A-Za-z.-]+ one of: “0” to “9”, “A” to “Z”, “a” to “z”, “.” or “-”, one or more times, as many as possible
  26. )? end of group, whole group optionally
  27. (?: a group, not capturing
  28. \+ the character “+”
  29. [0-9A-Za-z.-]+ one of: “0” to “9”, “A” to “Z”, “a” to “z”, “.” or “-”, one or more times, as many as possible
  30. )? end of group, whole group optionally
  31. \b a word boundary

Other patterns