|
18 | 18 | - Runtime is zero-dependency and ultra-minimal
|
19 | 19 | - Ships with transform for compiling runtime to pure RegExp
|
20 | 20 | - Supports automatically typed capture groups
|
21 |
| -- Packed with useful utilities: `charIn`, `charNotIn`, `anyOf`, `char`, `word`, `digit`, `whitespace`, `letter`, `tab`, `linefeed`, `carriageReturn`, `not`, `maybe`, `exactly` |
22 |
| -- All chainable with `and`, `or`, `after`, `before`, `notAfter`, `notBefore`, `times`, `as`, `at` |
| 21 | +- Packed with useful utilities: `charIn`, `charNotIn`, `anyOf`, `char`, `word`, `digit`, `whitespace`, `letter`, `tab`, `linefeed`, `carriageReturn`, `not`, `maybe`, `exactly`, `oneOrMore` |
| 22 | +- All chainable with `and`, `or`, `after`, `before`, `notAfter`, `notBefore`, `times`, `as`, `at`, `optionally` |
23 | 23 |
|
24 | 24 | **Future ideas**
|
25 | 25 |
|
@@ -75,14 +75,16 @@ They are:
|
75 | 75 | - `char`, `word`, `digit`, `whitespace`, `letter`, `tab`, `linefeed` and `carriageReturn` - these are helpers for specific RegExp characters.
|
76 | 76 | - `not` - this can prefix `word`, `digit`, `whitespace`, `letter`, `tab`, `linefeed` or `carriageReturn`. For example `createRegExp(not.letter)`.
|
77 | 77 | - `maybe` - equivalent to `?` - this marks the input as optional.
|
| 78 | +- `oneOrMore` - equivalent to `+` - this marks the input as repeatable, any number of times but at least once. |
78 | 79 | - `exactly` - this escapes a string input to match it exactly.
|
79 | 80 |
|
80 | 81 | All of these helpers return an object of type `Input` that can be chained with the following helpers:
|
81 | 82 |
|
82 | 83 | - `and` - this adds a new pattern to the current input.
|
83 | 84 | - `or` - this provides an alternative to the current input.
|
84 | 85 | - `after`, `before`, `notAfter` and `notBefore` - these activate positive/negative lookahead/lookbehinds. Make sure to check [browser support](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/RegExp#browser_compatibility) as not all browsers support lookbehinds (notably Safari).
|
85 |
| -- `times` - this is a function you can call directly to repeat the previous pattern an exact number of times, or you can use `times.between(min, max)` to specify a range. |
| 86 | +- `times` - this is a function you can call directly to repeat the previous pattern an exact number of times, or you can use `times.between(min, max)` to specify a range, `times.atLeast(num)` to indicate it must repeat x times or `times.any()` to indicate it can repeat any number of times, _including none_. |
| 87 | +- `optionally` - this is a function you can call to mark the current input as optional. |
86 | 88 | - `as` - this defines the entire input so far as a named capture group. You will get type safety when using the resulting RegExp with `String.match()`.
|
87 | 89 | - `at` - this allows you to match beginning/ends of lines with `at.lineStart()` and `at.lineEnd()`.
|
88 | 90 |
|
@@ -157,6 +159,22 @@ export default defineBuildConfig({
|
157 | 159 | })
|
158 | 160 | ```
|
159 | 161 |
|
| 162 | +## Examples |
| 163 | + |
| 164 | +```js |
| 165 | +import { createRegExp, exactly, oneOrMore, digit } from 'magic-regexp' |
| 166 | + |
| 167 | +// Quick-and-dirty semver |
| 168 | +createRegExp( |
| 169 | + oneOrMore(digit) |
| 170 | + .as('major') |
| 171 | + .and('.') |
| 172 | + .and(oneOrMore(digit).as('minor')) |
| 173 | + .and(exactly('.').and(oneOrMore(char).as('patch')).optionally()) |
| 174 | +) |
| 175 | +// /(?<major>(\d)+)\.(?<minor>(\d)+)(\.(?<patch>(.)+))?/ |
| 176 | +``` |
| 177 | + |
160 | 178 | ## 💻 Development
|
161 | 179 |
|
162 | 180 | - Clone this repository
|
|
0 commit comments