regex-partial-match 0.1.13 → 0.2.1

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.
Files changed (3) hide show
  1. package/README.md +14 -3
  2. package/lib/index.js +21 -7
  3. package/package.json +2 -2
package/README.md CHANGED
@@ -69,13 +69,15 @@ Such combinations have not been tested.
69
69
  ## Supported Features
70
70
 
71
71
  - 🔤 [Literal characters](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Regular_expressions/Literal_character)
72
- - 📋 [Character classes](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Regular_expressions/Character_class) (`[abc]`, `[^abc]`, `[a-z]`)
73
72
  - 🔣 [Character escapes](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Regular_expressions/Character_escape) (`\n`, `\t`, `\x61`, `\u0061`, `\u{1F600}`)
73
+ - 🧩 [Character class escapes](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Regular_expressions/Character_class_escape): `/\w+/`, `/\d{3}/`
74
74
  - 🌐 [Unicode character class escape](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Regular_expressions/Unicode_character_class_escape) (`\p{Letter}`, `\P{Letter}`)
75
+ - 📋 [Character classes](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Regular_expressions/Character_class) (`[abc]`, `[^abc]`, `[a-z]`)
76
+ - 🧮 [Unicode sets (`v` flag)](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/RegExp/unicodeSets) (`/[\p{Lowercase}&&\p{Script=Greek}]/v`)
75
77
  - 🔢 [Quantifiers](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Regular_expressions/Quantifier) (`*`, `+`, `?`, `{n}`, `{n,}`, `{n,m}`)
76
78
  - 🔀 [Disjunction](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Regular_expressions/Disjunction) (`a|b`)
77
79
  - 👥 [Groups](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Regular_expressions/Capturing_group) (capturing and non-capturing) (`(?:abc)`, `(abc)`, `(?<named>abc)`)
78
- - 👀 [Lookahead assertions](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Regular_expressions/Lookahead_assertion) (`(?=...)`, `(?!...)`)
80
+ - 👉 [Lookahead assertions](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Regular_expressions/Lookahead_assertion) (`(?=...)`, `(?!...)`)
79
81
  - 👈 [Lookbehind assertions](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Regular_expressions/Lookbehind_assertion) (`(?<=...)`, `(?<!...)`)
80
82
  - ⚓ [Input Boundaries](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Regular_expressions/Input_boundary_assertion) (`^`, `$`)
81
83
  - 🆒 [Word Boundaries](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Regular_expressions/Word_boundary_assertion) (`\b`, `\B`)
@@ -86,7 +88,7 @@ Such combinations have not been tested.
86
88
  The following regex features are **not currently supported**:
87
89
 
88
90
  - ⚠️ [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).
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).
91
+ - ⚠️ [Character class substrings](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Regular_expressions/Character_class#matching_strings) (`\q{abc}`) - When used independently, rather than to modify, can be included, but can't partially match. See [caveats](#caveats).
90
92
  - ❌ [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).
91
93
 
92
94
  ## Browser Compatibility
@@ -99,6 +101,7 @@ The library is compiled to **ES5** for broad compatibility with older browsers a
99
101
  - [**Named capturing groups**](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Regular_expressions/Named_capturing_group) (`(?<name>...)`) - ES2018+
100
102
  - [**`s` (dotAll) flag**](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/RegExp/dotAll) - ES2018+
101
103
  - [**`d` (hasIndices) flag**](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/RegExp/hasIndices) - ES2022+
104
+ - [**`v` (unicodeSets) flag**](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/RegExp/unicodeSets) - ES2024+
102
105
 
103
106
  ## Caveats
104
107
 
@@ -178,6 +181,14 @@ The **global flag is preserved** but may not be necessary for partial matching u
178
181
 
179
182
  The global flag does not cause issues like the sticky flag, as partial patterns naturally match from the beginning of the input. However, if you're using `lastIndex` to track position, be aware that failed matches will reset it to 0.
180
183
 
184
+ ### "String properties"
185
+
186
+ As with surrogate pair matching, grapheme clusters / string properties can only match atomically.
187
+
188
+ Hence, `[\p{RGI_Emoji_Flag_Sequence}]` will match `🇺🇳` as a whole, but not as the individual code points of which it's comprised.
189
+
190
+ In [`v` mode](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/RegExp/unicodeSets) expressions, where `[\q{abc}]` syntax is used in isolation (rather than its canonical use-case as a subtraction/intersection of another character class), this will also only match entirely or not at all. i.e. `abc` can match, but not partially.
191
+
181
192
  ## Examples
182
193
 
183
194
  ### Form Validation
package/lib/index.js CHANGED
@@ -1,7 +1,7 @@
1
- var characterClassGlobalRegex = /\[(?:\\.|.)*?\]/g;
2
1
  var occurrencesRegex = /\{\d+,?\d*\}/g;
3
2
  var createPartialMatchRegex = function (regex) {
4
3
  var source = regex.source;
4
+ var isUnicode = regex.unicode || regex.unicodeSets;
5
5
  var i = 0;
6
6
  function extractSubstring(length) {
7
7
  return source.substring(i, (i += length));
@@ -25,7 +25,7 @@ var createPartialMatchRegex = function (regex) {
25
25
  appendOptional(source.indexOf(">", i) - i + 1);
26
26
  break;
27
27
  case "u":
28
- if (regex.unicode && source[i + 2] === "{") {
28
+ if (isUnicode && source[i + 2] === "{") {
29
29
  appendOptional(source.indexOf("}", i) - i + 1);
30
30
  }
31
31
  else {
@@ -34,7 +34,7 @@ var createPartialMatchRegex = function (regex) {
34
34
  break;
35
35
  case "p":
36
36
  case "P":
37
- if (regex.unicode) {
37
+ if (isUnicode) {
38
38
  appendOptional(source.indexOf("}", i) - i + 1);
39
39
  }
40
40
  else {
@@ -50,10 +50,24 @@ var createPartialMatchRegex = function (regex) {
50
50
  }
51
51
  break;
52
52
  case "[": {
53
- characterClassGlobalRegex.lastIndex = i;
54
- // eslint-disable-next-line @typescript-eslint/no-non-null-assertion -- must match, since otherwise an invalid regex
55
- var match = characterClassGlobalRegex.exec(source)[0];
56
- appendOptional(match.length);
53
+ var depth = 1, escaped = false, j = i + 1;
54
+ while (depth) {
55
+ switch (source[j++]) {
56
+ case "\\":
57
+ escaped = !escaped;
58
+ continue;
59
+ case "[":
60
+ if (!escaped)
61
+ depth++;
62
+ break;
63
+ case "]":
64
+ if (!escaped)
65
+ depth--;
66
+ break;
67
+ }
68
+ escaped = false;
69
+ }
70
+ appendOptional(j - i);
57
71
  break;
58
72
  }
59
73
  case "|":
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "regex-partial-match",
3
- "version": "0.1.13",
3
+ "version": "0.2.1",
4
4
  "description": "A regular expression transform for partial matching",
5
5
  "devEngines": {
6
6
  "runtime": {
@@ -66,7 +66,7 @@
66
66
  },
67
67
  "lint-staged": {
68
68
  "*.{ts,js}": [
69
- "npm run lint:fix"
69
+ "eslint --fix"
70
70
  ]
71
71
  },
72
72
  "publishConfig": {