regex-partial-match 0.1.9 → 0.1.10
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 +11 -10
- package/lib/extend.d.ts +29 -0
- package/lib/index.d.ts +29 -0
- package/lib/index.js +29 -0
- package/package.json +1 -1
package/README.md
CHANGED
|
@@ -77,7 +77,8 @@ Such combinations have not been tested.
|
|
|
77
77
|
- 👥 [Groups](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Regular_expressions/Capturing_group) (capturing and non-capturing) (`(?:abc)`, `(abc)`, `(?<named>abc)`)
|
|
78
78
|
- 👀 [Lookahead assertions](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Regular_expressions/Lookahead_assertion) (`(?=...)`, `(?!...)`)
|
|
79
79
|
- 👈 [Lookbehind assertions](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Regular_expressions/Lookbehind_assertion) (`(?<=...)`, `(?<!...)`)
|
|
80
|
-
- ⚓ [
|
|
80
|
+
- ⚓ [Input Boundaries](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Regular_expressions/Input_boundary_assertion) (`^`, `$`)
|
|
81
|
+
- 🆒 [Word Boundaries](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Regular_expressions/Word_boundary_assertion) (`\b`, `\B`)
|
|
81
82
|
- 🏴 [Flags](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/RegExp/flags): `g`, `i`, `m`, `s`, `u`, `d`, `y` (See caveats for `y`)
|
|
82
83
|
|
|
83
84
|
## Unsupported Features
|
|
@@ -85,8 +86,8 @@ Such combinations have not been tested.
|
|
|
85
86
|
The following regex features are **not currently supported**:
|
|
86
87
|
|
|
87
88
|
- ⚠️ [Backreferences](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Regular_expressions/Backreference) (`\1`, `\k<name>`) - Can be included, but can't partially match. See [caveats](#caveats).
|
|
88
|
-
- ❌ [Unicode sets (`v` flag)](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/RegExp/unicodeSets) - ES2024
|
|
89
|
-
- ❌ [Modifiers](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Regular_expressions/Modifier) (`(?ims:...)`, `(?-ims:...)`) - ES2025
|
|
89
|
+
- ❌ [Unicode sets (`v` flag)](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/RegExp/unicodeSets) - ES2024+. See [issue](https://github.com/TomStrepsil/regex-partial-match/issues/1).
|
|
90
|
+
- ❌ [Modifiers](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Regular_expressions/Modifier) (`(?ims:...)`, `(?-ims:...)`) - ES2025+. See [issue](https://github.com/TomStrepsil/regex-partial-match/issues/2).
|
|
90
91
|
|
|
91
92
|
## Browser Compatibility
|
|
92
93
|
|
|
@@ -103,24 +104,24 @@ The library is compiled to **ES5** for broad compatibility with older browsers a
|
|
|
103
104
|
|
|
104
105
|
### [`.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
|
|
|
106
|
-
The library produces an expression that always matches an empty string, at the end of the input.
|
|
107
|
+
The library produces an expression that always matches an empty string, at the end of the input. Feasibly, this is the start of a new partial match.
|
|
107
108
|
|
|
108
109
|
Hence:
|
|
109
110
|
|
|
110
111
|
```js
|
|
111
|
-
/x/.test("a") === false; /*
|
|
112
|
+
/x/.test("a") === false; /* untransformed regex */
|
|
112
113
|
/(?:x|$)/.test("a") === true; /* what's produced by the library */
|
|
113
114
|
```
|
|
114
115
|
|
|
115
|
-
To mitigate, a start boundary anchor can prevent anything _but_
|
|
116
|
+
To mitigate, a start boundary anchor can prevent anything _but_ an empty string matching:
|
|
116
117
|
|
|
117
118
|
```js
|
|
118
119
|
/^(?:x|$)/.test("") === true;
|
|
119
|
-
/^(?:x|$)/.test("a") === false;
|
|
120
120
|
/^(?:x|$)/.test("x") === true;
|
|
121
|
+
/^(?:x|$)/.test("a") === false;
|
|
121
122
|
```
|
|
122
123
|
|
|
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
|
+
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", if validating that which came before.
|
|
124
125
|
|
|
125
126
|
i.e.
|
|
126
127
|
|
|
@@ -129,7 +130,7 @@ i.e.
|
|
|
129
130
|
"a".match(/(?:x|$)/); // ['', index: 1, input: "a", groups: undefined];
|
|
130
131
|
```
|
|
131
132
|
|
|
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
|
+
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. See [issue](https://github.com/TomStrepsil/regex-partial-match/issues/14).
|
|
133
134
|
|
|
134
135
|
### Backreferences
|
|
135
136
|
|
|
@@ -151,7 +152,7 @@ e.g.
|
|
|
151
152
|
|
|
152
153
|
### Surrogate Pair Matching
|
|
153
154
|
|
|
154
|
-
In unicode-aware mode (`u` flag), **only whole astral characters are supported**. Partial matching of individual surrogate pairs is not supported. For example, `/😀/u` will match the complete emoji character, but not the first surrogate pair in isolation. Hence, if partially matching a byte stream, be sure to pipe via a [`TextDecoder`](https://developer.mozilla.org/en-US/docs/Web/API/TextDecoder) first.
|
|
155
|
+
In [unicode-aware mode](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/RegExp/unicode) (`u` flag), **only whole astral characters are supported**. Partial matching of individual surrogate pairs is not supported. For example, `/😀/u` will match the complete emoji character, but not the first surrogate pair in isolation. Hence, if partially matching a byte stream, be sure to pipe via a [`TextDecoder`](https://developer.mozilla.org/en-US/docs/Web/API/TextDecoder) first.
|
|
155
156
|
|
|
156
157
|
### [Sticky](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/RegExp/sticky) Flag (`y`)
|
|
157
158
|
|
package/lib/extend.d.ts
CHANGED
|
@@ -1,5 +1,34 @@
|
|
|
1
1
|
declare global {
|
|
2
2
|
interface RegExp {
|
|
3
|
+
/**
|
|
4
|
+
* Transforms this regular expression to support partial matching.
|
|
5
|
+
*
|
|
6
|
+
* This method wraps each atomic element of the regex pattern in a non-capturing group
|
|
7
|
+
* with an alternation to end-of-input (`$`), allowing the pattern to match prefixes
|
|
8
|
+
* of the original pattern. This enables validation of incomplete input strings.
|
|
9
|
+
*
|
|
10
|
+
* @returns A new RegExp that matches partial strings of the original pattern
|
|
11
|
+
*
|
|
12
|
+
* @example
|
|
13
|
+
* ```typescript
|
|
14
|
+
* import 'regex-partial-match/extend';
|
|
15
|
+
*
|
|
16
|
+
* const partial = /hello world/.toPartialMatchRegex();
|
|
17
|
+
*
|
|
18
|
+
* partial.test('h'); // true - could match
|
|
19
|
+
* partial.test('hello'); // true - could match
|
|
20
|
+
* partial.test('hello world'); // true - full match
|
|
21
|
+
* partial.test('goodbye'); // false - cannot match
|
|
22
|
+
* ```
|
|
23
|
+
*
|
|
24
|
+
* @remarks
|
|
25
|
+
* - The transformed regex will always match an empty string at the end of input
|
|
26
|
+
* - Backreferences cannot be partially matched as they are atomic
|
|
27
|
+
* - Use with a start anchor (`^`) to prevent false positives from empty string matches
|
|
28
|
+
* - The `y` (sticky) flag may not behave as expected in partial matching scenarios
|
|
29
|
+
*
|
|
30
|
+
* @see {@link https://github.com/TomStrepsil/regex-partial-match#readme | Documentation}
|
|
31
|
+
*/
|
|
3
32
|
toPartialMatchRegex(): RegExp;
|
|
4
33
|
}
|
|
5
34
|
}
|
package/lib/index.d.ts
CHANGED
|
@@ -1,2 +1,31 @@
|
|
|
1
1
|
declare const createPartialMatchRegex: (regex: RegExp) => RegExp;
|
|
2
|
+
/**
|
|
3
|
+
* Transforms a regular expression to support partial matching.
|
|
4
|
+
*
|
|
5
|
+
* This function wraps each atomic element of the regex pattern in a non-capturing group
|
|
6
|
+
* with an alternation to end-of-input (`$`), allowing the pattern to match prefixes
|
|
7
|
+
* of the original pattern. This enables validation of incomplete input strings.
|
|
8
|
+
*
|
|
9
|
+
* @param regex - The regular expression to transform for partial matching
|
|
10
|
+
* @returns A new RegExp that matches partial strings of the original pattern
|
|
11
|
+
*
|
|
12
|
+
* @example
|
|
13
|
+
* ```typescript
|
|
14
|
+
* const pattern = /hello world/;
|
|
15
|
+
* const partial = createPartialMatchRegex(pattern);
|
|
16
|
+
*
|
|
17
|
+
* partial.test('h'); // true - could match
|
|
18
|
+
* partial.test('hello'); // true - could match
|
|
19
|
+
* partial.test('hello world'); // true - full match
|
|
20
|
+
* partial.test('goodbye'); // false - cannot match
|
|
21
|
+
* ```
|
|
22
|
+
*
|
|
23
|
+
* @remarks
|
|
24
|
+
* - The transformed regex will always match an empty string at the end of input
|
|
25
|
+
* - Backreferences cannot be partially matched as they are atomic
|
|
26
|
+
* - Use with a start anchor (`^`) to prevent false positives from empty string matches
|
|
27
|
+
* - The `y` (sticky) flag may not behave as expected in partial matching scenarios
|
|
28
|
+
*
|
|
29
|
+
* @see {@link https://github.com/TomStrepsil/regex-partial-match#readme | Documentation}
|
|
30
|
+
*/
|
|
2
31
|
export default createPartialMatchRegex;
|
package/lib/index.js
CHANGED
|
@@ -130,4 +130,33 @@ var createPartialMatchRegex = function (regex) {
|
|
|
130
130
|
}
|
|
131
131
|
return new RegExp(process(), regex.flags);
|
|
132
132
|
};
|
|
133
|
+
/**
|
|
134
|
+
* Transforms a regular expression to support partial matching.
|
|
135
|
+
*
|
|
136
|
+
* This function wraps each atomic element of the regex pattern in a non-capturing group
|
|
137
|
+
* with an alternation to end-of-input (`$`), allowing the pattern to match prefixes
|
|
138
|
+
* of the original pattern. This enables validation of incomplete input strings.
|
|
139
|
+
*
|
|
140
|
+
* @param regex - The regular expression to transform for partial matching
|
|
141
|
+
* @returns A new RegExp that matches partial strings of the original pattern
|
|
142
|
+
*
|
|
143
|
+
* @example
|
|
144
|
+
* ```typescript
|
|
145
|
+
* const pattern = /hello world/;
|
|
146
|
+
* const partial = createPartialMatchRegex(pattern);
|
|
147
|
+
*
|
|
148
|
+
* partial.test('h'); // true - could match
|
|
149
|
+
* partial.test('hello'); // true - could match
|
|
150
|
+
* partial.test('hello world'); // true - full match
|
|
151
|
+
* partial.test('goodbye'); // false - cannot match
|
|
152
|
+
* ```
|
|
153
|
+
*
|
|
154
|
+
* @remarks
|
|
155
|
+
* - The transformed regex will always match an empty string at the end of input
|
|
156
|
+
* - Backreferences cannot be partially matched as they are atomic
|
|
157
|
+
* - Use with a start anchor (`^`) to prevent false positives from empty string matches
|
|
158
|
+
* - The `y` (sticky) flag may not behave as expected in partial matching scenarios
|
|
159
|
+
*
|
|
160
|
+
* @see {@link https://github.com/TomStrepsil/regex-partial-match#readme | Documentation}
|
|
161
|
+
*/
|
|
133
162
|
export default createPartialMatchRegex;
|