regex-partial-match 0.1.6 → 0.1.9
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +51 -19
- package/package.json +2 -1
package/README.md
CHANGED
|
@@ -101,19 +101,53 @@ The library is compiled to **ES5** for broad compatibility with older browsers a
|
|
|
101
101
|
|
|
102
102
|
## Caveats
|
|
103
103
|
|
|
104
|
+
### [`.test()`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/RegExp/test) behaviour and non-matching results from [`.exec()`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/RegExp/exec) and [`.match()`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String/match)
|
|
105
|
+
|
|
106
|
+
The library produces an expression that always matches an empty string, at the end of the input.
|
|
107
|
+
|
|
108
|
+
Hence:
|
|
109
|
+
|
|
110
|
+
```js
|
|
111
|
+
/x/.test("a") === false; /* what you'd expect */
|
|
112
|
+
/(?:x|$)/.test("a") === true; /* what's produced by the library */
|
|
113
|
+
```
|
|
114
|
+
|
|
115
|
+
To mitigate, a start boundary anchor can prevent anything _but_ the empty string matching:
|
|
116
|
+
|
|
117
|
+
```js
|
|
118
|
+
/^(?:x|$)/.test("") === true;
|
|
119
|
+
/^(?:x|$)/.test("a") === false;
|
|
120
|
+
/^(?:x|$)/.test("x") === true;
|
|
121
|
+
```
|
|
122
|
+
|
|
123
|
+
On this basis, `.test()` should be used with caution, and a match of an empty string at the end of the input should instead be considered "no match".
|
|
124
|
+
|
|
125
|
+
i.e.
|
|
126
|
+
|
|
127
|
+
```js
|
|
128
|
+
/(?:x|$)/.exec("a"); // ['', index: 1, input: "a", groups: undefined];
|
|
129
|
+
"a".match(/(?:x|$)/); // ['', index: 1, input: "a", groups: undefined];
|
|
130
|
+
```
|
|
131
|
+
|
|
132
|
+
Since the library produces a native `RegExp` object, no attempt to proxy / translate this output to `null` has been attempted, but a helper could be produced in future, for clarity.
|
|
133
|
+
|
|
104
134
|
### Backreferences
|
|
105
135
|
|
|
106
|
-
**Backreferences
|
|
136
|
+
**Backreferences cannot be partially matched because they are atomic.** A backreference like `\1` must match the complete captured text or fail entirely, and cannot be split into individual characters for partial matching like regular atoms can.
|
|
137
|
+
|
|
138
|
+
Fixed-length patterns like `/(abc)\1/` could theoretically become `/(?:(a)|$)(?:(b)|$)(?:(c)|$)(?:\1|$)(?:\2|$)(?:\3|$)/` (accepting polluted capture indexes as a side-effect), but this doesn't work for variable-length captures.
|
|
139
|
+
|
|
140
|
+
### Positive Lookbehinds
|
|
141
|
+
|
|
142
|
+
Whilst forming a match, a positive lookbehind must match in entirety, for the pattern to match. This is inherent in the concept of non-matching groups, since they are not match-worthy themselves, but just qualify matching atoms.
|
|
107
143
|
|
|
108
|
-
|
|
144
|
+
e.g.
|
|
109
145
|
|
|
110
|
-
|
|
111
|
-
|
|
112
|
-
|
|
113
|
-
- Capture group 1 as "h", skip the space with `$`, match `\1` as "h" ✓ (at position 6)
|
|
114
|
-
- The engine chooses the second interpretation, matching just "h" instead of recognizing "hello h" as a partial match
|
|
146
|
+
```js
|
|
147
|
+
/(?<=foo)bar/;
|
|
148
|
+
```
|
|
115
149
|
|
|
116
|
-
|
|
150
|
+
"f" through "foo" is not a match, but "foob" is.
|
|
117
151
|
|
|
118
152
|
### Surrogate Pair Matching
|
|
119
153
|
|
|
@@ -148,8 +182,6 @@ The global flag does not cause issues like the sticky flag, as partial patterns
|
|
|
148
182
|
### Form Validation
|
|
149
183
|
|
|
150
184
|
```javascript
|
|
151
|
-
import createPartialMatchRegex from "regex-partial-match";
|
|
152
|
-
|
|
153
185
|
const emailPattern = /^[a-z0-9._%+-]+@[a-z0-9.-]+\.[a-z]{2,}$/i;
|
|
154
186
|
const partial = createPartialMatchRegex(emailPattern);
|
|
155
187
|
|
|
@@ -183,8 +215,6 @@ getSuggestions("xyz"); // 'no suggestions'
|
|
|
183
215
|
### Stream Processing
|
|
184
216
|
|
|
185
217
|
```javascript
|
|
186
|
-
import createPartialMatchRegex from "regex-partial-match";
|
|
187
|
-
|
|
188
218
|
// Process streaming data with pattern matching at chunk boundaries
|
|
189
219
|
const pattern = /\{"[^"]+":"[^"]+"\}/; // Match JSON objects
|
|
190
220
|
const partial = createPartialMatchRegex(pattern);
|
|
@@ -254,11 +284,13 @@ Algorithm created by [Lucas Trzesniewski](https://github.com/ltrzesniewski).
|
|
|
254
284
|
|
|
255
285
|
Contributions are welcome! Please open an issue or pull request on [GitHub](https://github.com/TomStrepsil/regex-partial-match).
|
|
256
286
|
|
|
257
|
-
## Related
|
|
287
|
+
## Related projects
|
|
258
288
|
|
|
259
|
-
|
|
260
|
-
|
|
261
|
-
|
|
262
|
-
|
|
263
|
-
|
|
264
|
-
|
|
289
|
+
| Project | Description |
|
|
290
|
+
| -------------------------------------------------------------------------- | ------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------ |
|
|
291
|
+
| [`incr-regex-package`](https://www.npmjs.com/package/incr-regex-package) | Incremental regex matcher |
|
|
292
|
+
| [`dfa`](https://github.com/foliojs/dfa) | Compiles a regular expression like syntax to fast deterministic finite automata, which could be used to partial match? |
|
|
293
|
+
| [`refa`](https://github.com/RunDevelopment/refa) | Can [convert regular expressions to an Abstract Syntax Tree](https://rundevelopment.github.io/refa/docs/latest/classes/JS.Parser.html), which might afford partial-match capability? |
|
|
294
|
+
| [`@eslint-community/regexpp`](https://github.com/eslint-community/regexpp) | A regular expression parser for ECMAScript with AST generation and visitor implementation |
|
|
295
|
+
| [`Regex+`](https://www.npmjs.com/package/regex) | template literal, transforming native regular expressions |
|
|
296
|
+
| [`Awesome Regex`](https://github.com/slevithan/awesome-regex) | Curated list of tools, tutorials, libraries, and other resources, covering all major regex flavours |
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "regex-partial-match",
|
|
3
|
-
"version": "0.1.
|
|
3
|
+
"version": "0.1.9",
|
|
4
4
|
"description": "A regular expression transform for partial matching",
|
|
5
5
|
"engines": {
|
|
6
6
|
"node": ">=23"
|
|
@@ -66,6 +66,7 @@
|
|
|
66
66
|
},
|
|
67
67
|
"devDependencies": {
|
|
68
68
|
"@eslint/js": "^9.17.0",
|
|
69
|
+
"@eslint/markdown": "^7.5.1",
|
|
69
70
|
"eslint": "^9.17.0",
|
|
70
71
|
"jiti": "^2.6.1",
|
|
71
72
|
"lint-staged": "^15.2.11",
|