Skip to content

Instantly share code, notes, and snippets.

@mikybars
Last active March 2, 2023 23:31
Show Gist options
  • Save mikybars/2138045067fcd2d244e9959745cb1e86 to your computer and use it in GitHub Desktop.
Save mikybars/2138045067fcd2d244e9959745cb1e86 to your computer and use it in GitHub Desktop.
🦄 magic-regexp: A compiled-away, type-safe, readable RegExp alternative.
// https://regexp.dev
import { createRegExp, exactly, oneOrMore, digit, char } from 'magic-regexp'
// hover to see the compiled regexp
const semver = createRegExp(
oneOrMore(digit)
.groupedAs('major')
.and('.')
.and(oneOrMore(digit).groupedAs('minor'))
.and(exactly('.').and(oneOrMore(char).groupedAs('patch')).optionally())
)
console.log(semver)
// /(?<major>\d+)\.(?<minor>\d+)(?:\.(?<patch>.+))?/
const { major, minor, patch } = '2.1.3-RELEASE'.match(semver)?.groups
console.log(major, minor, patch)
semver.test('0.0.1')
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment