You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: README.md
+47-5Lines changed: 47 additions & 5 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -28,7 +28,7 @@
28
28
-[ ] Instrumentation for accurately getting coverage on RegExps
29
29
-[ ] Hybrid/partially-compiled RegExps for better dynamic support
30
30
31
-
## Usage
31
+
## Setup
32
32
33
33
Install package:
34
34
@@ -52,9 +52,51 @@ console.log(regExp)
52
52
// /(?<=bar\/)foo\/test\.js/
53
53
```
54
54
55
-
In order to statically transform magic-regexps at build time, you can use the included unplugin.
55
+
## Usage
56
+
57
+
Every regular expression you create with the library should be wrapped in `createRegExp`.
58
+
59
+
> **Note**
60
+
> By default, all helpers from `magic-regexp` assume that input that is passed should be escaped - so no special RegExp characters apply. So `createRegExp('foo?\d')` will not match `food3` but only `foo?\d` exactly.
61
+
62
+
There are a range of helpers that can be used to activate pattern matching, and they can be chained.
63
+
64
+
They are:
65
+
66
+
-`charIn`, `charNotIn` - this matches or doesn't match any character in the string provided.
67
+
-`anyOf` - this takes an array of inputs and matches any of them.
68
+
-`char`, `word`, `digit`, `whitespace`, `letter`, `tab`, `linefeed` and `carriageReturn` - these are helpers for specific RegExp characters.
69
+
-`not` - this can prefix `word`, `digit`, `whitespace`, `letter`, `tab`, `linefeed` or `carriageReturn`. For example `createRegExp(not.letter)`.
70
+
-`maybe` - equivalent to `?` - this marks the input as optional.
71
+
-`exactly` - this escapes a string input to match it exactly.
72
+
73
+
All of these helpers return an object of type `Input` that can be chained with the following helpers:
74
+
75
+
-`and` - this adds a new pattern to the current input.
76
+
-`or` - this provides an alternative to the current input.
77
+
-`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).
78
+
-`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.
79
+
-`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()`.
80
+
-`at` - this allows you to match beginning/ends of lines with `at.lineStart()` and `at.lineEnd()`.
81
+
82
+
## Compilation at build
83
+
84
+
The best way to use `magic-regexp` is by making use of the included transform.
Of course, this only works with non-dynamic regexps. Within the `createRegExp` block you have to include all the helpers you are using from `magic-regexp` - and not rely on any external variables. This, for example, will not statically compile into a RegExp, although it will still continue to work with a minimal runtime:
0 commit comments