Last active
March 2, 2023 23:31
-
-
Save mikybars/2138045067fcd2d244e9959745cb1e86 to your computer and use it in GitHub Desktop.
🦄 magic-regexp: A compiled-away, type-safe, readable RegExp alternative.
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
// 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