re2js 1.3.3 → 2.0.0
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 +39 -17
- package/build/index.cjs.cjs +51 -22
- package/build/index.cjs.cjs.map +1 -1
- package/build/index.esm.d.ts +23 -11
- package/build/index.esm.d.ts.map +1 -1
- package/build/index.esm.js +51 -22
- package/build/index.esm.js.map +1 -1
- package/build/index.umd.js +51 -22
- package/build/index.umd.js.map +1 -1
- package/package.json +1 -1
package/build/index.esm.d.ts
CHANGED
|
@@ -26,9 +26,10 @@ export class Matcher {
|
|
|
26
26
|
* {@link #appendReplacement} as a literal replacement of {@code s}.
|
|
27
27
|
*
|
|
28
28
|
* @param {string} str the string to be quoted
|
|
29
|
+
* @param {boolean} [javaMode=false] whether the replacement will be used in javaMode
|
|
29
30
|
* @returns {string} the quoted string
|
|
30
31
|
*/
|
|
31
|
-
static quoteReplacement(str: string): string;
|
|
32
|
+
static quoteReplacement(str: string, javaMode?: boolean): string;
|
|
32
33
|
/**
|
|
33
34
|
*
|
|
34
35
|
* @param {RE2JS} pattern
|
|
@@ -170,7 +171,7 @@ export class Matcher {
|
|
|
170
171
|
* earlier, escape the first digit that should not be used.
|
|
171
172
|
*
|
|
172
173
|
* @param {string} replacement the replacement string
|
|
173
|
-
* @param {boolean} [
|
|
174
|
+
* @param {boolean} [javaMode=false] activate java mode (different behaviour for capture groups and special characters)
|
|
174
175
|
* @returns {string}
|
|
175
176
|
* @throws IllegalStateException if there was no most recent match
|
|
176
177
|
* @throws IndexOutOfBoundsException if replacement refers to an invalid group
|
|
@@ -182,13 +183,13 @@ export class Matcher {
|
|
|
182
183
|
* @returns {string}
|
|
183
184
|
* @private
|
|
184
185
|
*/
|
|
185
|
-
private
|
|
186
|
+
private appendReplacementInternalJava;
|
|
186
187
|
/**
|
|
187
188
|
* @param {string} replacement - the replacement string
|
|
188
189
|
* @returns {string}
|
|
189
190
|
* @private
|
|
190
191
|
*/
|
|
191
|
-
private
|
|
192
|
+
private appendReplacementInternalJs;
|
|
192
193
|
/**
|
|
193
194
|
* Return the substring of the input from the append position to the end of the
|
|
194
195
|
* input.
|
|
@@ -200,26 +201,26 @@ export class Matcher {
|
|
|
200
201
|
* {@code appendReplacement}.
|
|
201
202
|
*
|
|
202
203
|
* @param {string} replacement - the replacement string
|
|
203
|
-
* @param {boolean} [
|
|
204
|
+
* @param {boolean} [javaMode=false] - activate java mode (different behaviour for capture groups and special characters)
|
|
204
205
|
* @returns {string} the input string with the matches replaced
|
|
205
|
-
* @throws IndexOutOfBoundsException if replacement refers to an invalid group and
|
|
206
|
+
* @throws IndexOutOfBoundsException if replacement refers to an invalid group and javaMode is true
|
|
206
207
|
*/
|
|
207
|
-
replaceAll(replacement: string,
|
|
208
|
+
replaceAll(replacement: string, javaMode?: boolean): string;
|
|
208
209
|
/**
|
|
209
210
|
* Returns the input with the first match replaced by {@code replacement}, interpreted as for
|
|
210
211
|
* {@code appendReplacement}.
|
|
211
212
|
*
|
|
212
213
|
* @param {string} replacement - the replacement string
|
|
213
|
-
* @param {boolean} [
|
|
214
|
+
* @param {boolean} [javaMode=false] - activate java mode (different behaviour for capture groups and special characters)
|
|
214
215
|
* @returns {string} the input string with the first match replaced
|
|
215
|
-
* @throws IndexOutOfBoundsException if replacement refers to an invalid group and
|
|
216
|
+
* @throws IndexOutOfBoundsException if replacement refers to an invalid group and javaMode is true
|
|
216
217
|
*/
|
|
217
|
-
replaceFirst(replacement: string,
|
|
218
|
+
replaceFirst(replacement: string, javaMode?: boolean): string;
|
|
218
219
|
/**
|
|
219
220
|
* Helper: replaceAll/replaceFirst hybrid.
|
|
220
221
|
* @param {string} replacement - the replacement string
|
|
221
222
|
* @param {boolean} [all=true] - replace all matches
|
|
222
|
-
* @param {boolean} [
|
|
223
|
+
* @param {boolean} [javaMode=false] - activate java mode (different behaviour for capture groups and special characters)
|
|
223
224
|
* @returns {string}
|
|
224
225
|
* @private
|
|
225
226
|
*/
|
|
@@ -269,6 +270,17 @@ export class RE2JS {
|
|
|
269
270
|
* @returns {string} A literal string replacement
|
|
270
271
|
*/
|
|
271
272
|
static quote(str: string): string;
|
|
273
|
+
/**
|
|
274
|
+
* Quotes '\' and '$' in {@code str}, so that the returned string could be used in
|
|
275
|
+
* replacement methods as a literal replacement of {@code str}.
|
|
276
|
+
*
|
|
277
|
+
* This is a convenience delegation to {@link Matcher.quoteReplacement}.
|
|
278
|
+
*
|
|
279
|
+
* @param {string} str the string to be quoted
|
|
280
|
+
* @param {boolean} [javaMode=false] whether the replacement will be used in javaMode
|
|
281
|
+
* @returns {string} the quoted string
|
|
282
|
+
*/
|
|
283
|
+
static quoteReplacement(str: string, javaMode?: boolean): string;
|
|
272
284
|
/**
|
|
273
285
|
* Translates a given regular expression string to ensure compatibility with RE2JS.
|
|
274
286
|
*
|
package/build/index.esm.d.ts.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"index.esm.d.ts","sourceRoot":"","sources":["index.esm.js"],"names":[],"mappings":"AA0kCA;;;;;;;;;;;;;;;;;;;;;GAqBG;AACH;IACE
|
|
1
|
+
{"version":3,"file":"index.esm.d.ts","sourceRoot":"","sources":["index.esm.js"],"names":[],"mappings":"AA0kCA;;;;;;;;;;;;;;;;;;;;;GAqBG;AACH;IACE;;;;;;;OAOG;IACH,6BAJW,MAAM,aACN,OAAO,GACL,MAAM,CA2BlB;IACD;;;;OAIG;IACH,qBAHW,KAAK,SACL,gBAAgB,GAAC,iBAAiB,GAAC,MAAM,EAAE,GAAC,MAAM,EA0B5D;IAnBC,oBAA2B;IAG3B,qBAAqB;IACrB,mBADW,MAAM,CACqC;IAEtD,uBAAuB;IACvB,QADW,MAAM,EAAE,CACH;IAChB,qCAAqC;IACrC,aADW,MAAM,CAAC,MAAM,EAAE,MAAM,CAAC,CACC;IAClC,qBAAqB;IACrB,sBADW,MAAM,CACqC;IAUxD;;;OAGG;IACH,WAFa,KAAK,CAIjB;IAED;;;;OAI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MAAM,EAIjB;CACF;AA+DD;;GAEG;AACH;CAMC;AApBD;;GAEG;AACH;CAMC;AA3DD;;GAEG;AACH;IACE;;;OAGG;IACH,mBAHW,MAAM,UACN,MAAM,GAAC,IAAI,EAcrB;IAJC,qBAAqB;IACrB,OADW,MAAM,CACC;IAClB,0BAA0B;IAC1B,OADW,MAAM,GAAC,IAAI,CACJ;IAGpB;;;OAGG;IACH,kBAFa,MAAM,CAIlB;IAED;;;OAGG;IACH,cAFa,MAAM,GAAC,IAAI,CAIvB;CACF;AAlTD;IACE,0BAGC;IADC,WAAkB;IAEpB,mBAEC;IACD;;;OAGG;IACH,kBAFa,MAAM,CAIlB;IAED;;;OAGG;IACH,WAFa,MAAM,EAAE,CAIpB;IAED;;;OAGG;IACH,UAFa,MAAM,CAIlB;CACF;AACD;IACE,iCAGC;IADC,kBAAgC;IAElC,mBAEC;IAED;;;OAGG;IACH,kBAFa,MAAM,CAIlB;IAED;;;OAGG;IACH,WAFa,MAAM,EAAE,CAIpB;IAED;;;OAGG;IACH,UAFa,MAAM,CAIlB;CACF;AAzFD;;GAEG;AACH;IACE,8BAAkD;IAClD,oBAEC;IAED;;;OAGG;IACH,kBAFa,OAAO,CAInB;IAED;;;OAGG;IACH,mBAFa,OAAO,CAInB;CACF"}
|
package/build/index.esm.js
CHANGED
|
@@ -2,7 +2,7 @@
|
|
|
2
2
|
* re2js
|
|
3
3
|
* RE2JS is the JavaScript port of RE2, a regular expression engine that provides linear time matching
|
|
4
4
|
*
|
|
5
|
-
* @version
|
|
5
|
+
* @version v2.0.0
|
|
6
6
|
* @author Alexey Vasiliev
|
|
7
7
|
* @homepage https://github.com/le0pard/re2js#readme
|
|
8
8
|
* @repository github:le0pard/re2js
|
|
@@ -1124,16 +1124,31 @@ class Matcher {
|
|
|
1124
1124
|
* {@link #appendReplacement} as a literal replacement of {@code s}.
|
|
1125
1125
|
*
|
|
1126
1126
|
* @param {string} str the string to be quoted
|
|
1127
|
+
* @param {boolean} [javaMode=false] whether the replacement will be used in javaMode
|
|
1127
1128
|
* @returns {string} the quoted string
|
|
1128
1129
|
*/
|
|
1129
|
-
static quoteReplacement(str) {
|
|
1130
|
-
if (
|
|
1130
|
+
static quoteReplacement(str, javaMode = false) {
|
|
1131
|
+
if (javaMode) {
|
|
1132
|
+
// Java mode escape '\' and '$' with a backslash
|
|
1133
|
+
if (str.indexOf('\\') < 0 && str.indexOf('$') < 0) {
|
|
1134
|
+
return str;
|
|
1135
|
+
}
|
|
1136
|
+
return str.split('').map(s => {
|
|
1137
|
+
const c = s.codePointAt(0);
|
|
1138
|
+
if (c === Codepoint.CODES.get('\\') || c === Codepoint.CODES.get('$')) {
|
|
1139
|
+
return `\\${s}`;
|
|
1140
|
+
}
|
|
1141
|
+
return s;
|
|
1142
|
+
}).join('');
|
|
1143
|
+
}
|
|
1144
|
+
|
|
1145
|
+
// In JS mode, '\' is not a special character, but '$' must be escaped as '$$'
|
|
1146
|
+
if (str.indexOf('$') < 0) {
|
|
1131
1147
|
return str;
|
|
1132
1148
|
}
|
|
1133
1149
|
return str.split('').map(s => {
|
|
1134
|
-
|
|
1135
|
-
|
|
1136
|
-
return `\\${s}`;
|
|
1150
|
+
if (s.codePointAt(0) === Codepoint.CODES.get('$')) {
|
|
1151
|
+
return '$$';
|
|
1137
1152
|
}
|
|
1138
1153
|
return s;
|
|
1139
1154
|
}).join('');
|
|
@@ -1423,13 +1438,13 @@ class Matcher {
|
|
|
1423
1438
|
* earlier, escape the first digit that should not be used.
|
|
1424
1439
|
*
|
|
1425
1440
|
* @param {string} replacement the replacement string
|
|
1426
|
-
* @param {boolean} [
|
|
1441
|
+
* @param {boolean} [javaMode=false] activate java mode (different behaviour for capture groups and special characters)
|
|
1427
1442
|
* @returns {string}
|
|
1428
1443
|
* @throws IllegalStateException if there was no most recent match
|
|
1429
1444
|
* @throws IndexOutOfBoundsException if replacement refers to an invalid group
|
|
1430
1445
|
* @private
|
|
1431
1446
|
*/
|
|
1432
|
-
appendReplacement(replacement,
|
|
1447
|
+
appendReplacement(replacement, javaMode = false) {
|
|
1433
1448
|
let res = '';
|
|
1434
1449
|
const s = this.start();
|
|
1435
1450
|
const e = this.end();
|
|
@@ -1437,7 +1452,7 @@ class Matcher {
|
|
|
1437
1452
|
res += this.substring(this.appendPos, s);
|
|
1438
1453
|
}
|
|
1439
1454
|
this.appendPos = e;
|
|
1440
|
-
res +=
|
|
1455
|
+
res += javaMode ? this.appendReplacementInternalJava(replacement) : this.appendReplacementInternalJs(replacement);
|
|
1441
1456
|
return res;
|
|
1442
1457
|
}
|
|
1443
1458
|
|
|
@@ -1446,7 +1461,7 @@ class Matcher {
|
|
|
1446
1461
|
* @returns {string}
|
|
1447
1462
|
* @private
|
|
1448
1463
|
*/
|
|
1449
|
-
|
|
1464
|
+
appendReplacementInternalJava(replacement) {
|
|
1450
1465
|
let res = '';
|
|
1451
1466
|
let last = 0;
|
|
1452
1467
|
const m = replacement.length;
|
|
@@ -1512,7 +1527,7 @@ class Matcher {
|
|
|
1512
1527
|
* @returns {string}
|
|
1513
1528
|
* @private
|
|
1514
1529
|
*/
|
|
1515
|
-
|
|
1530
|
+
appendReplacementInternalJs(replacement) {
|
|
1516
1531
|
let res = '';
|
|
1517
1532
|
let last = 0;
|
|
1518
1533
|
const m = replacement.length;
|
|
@@ -1609,12 +1624,12 @@ class Matcher {
|
|
|
1609
1624
|
* {@code appendReplacement}.
|
|
1610
1625
|
*
|
|
1611
1626
|
* @param {string} replacement - the replacement string
|
|
1612
|
-
* @param {boolean} [
|
|
1627
|
+
* @param {boolean} [javaMode=false] - activate java mode (different behaviour for capture groups and special characters)
|
|
1613
1628
|
* @returns {string} the input string with the matches replaced
|
|
1614
|
-
* @throws IndexOutOfBoundsException if replacement refers to an invalid group and
|
|
1629
|
+
* @throws IndexOutOfBoundsException if replacement refers to an invalid group and javaMode is true
|
|
1615
1630
|
*/
|
|
1616
|
-
replaceAll(replacement,
|
|
1617
|
-
return this.replace(replacement, true,
|
|
1631
|
+
replaceAll(replacement, javaMode = false) {
|
|
1632
|
+
return this.replace(replacement, true, javaMode);
|
|
1618
1633
|
}
|
|
1619
1634
|
|
|
1620
1635
|
/**
|
|
@@ -1622,27 +1637,27 @@ class Matcher {
|
|
|
1622
1637
|
* {@code appendReplacement}.
|
|
1623
1638
|
*
|
|
1624
1639
|
* @param {string} replacement - the replacement string
|
|
1625
|
-
* @param {boolean} [
|
|
1640
|
+
* @param {boolean} [javaMode=false] - activate java mode (different behaviour for capture groups and special characters)
|
|
1626
1641
|
* @returns {string} the input string with the first match replaced
|
|
1627
|
-
* @throws IndexOutOfBoundsException if replacement refers to an invalid group and
|
|
1642
|
+
* @throws IndexOutOfBoundsException if replacement refers to an invalid group and javaMode is true
|
|
1628
1643
|
*/
|
|
1629
|
-
replaceFirst(replacement,
|
|
1630
|
-
return this.replace(replacement, false,
|
|
1644
|
+
replaceFirst(replacement, javaMode = false) {
|
|
1645
|
+
return this.replace(replacement, false, javaMode);
|
|
1631
1646
|
}
|
|
1632
1647
|
|
|
1633
1648
|
/**
|
|
1634
1649
|
* Helper: replaceAll/replaceFirst hybrid.
|
|
1635
1650
|
* @param {string} replacement - the replacement string
|
|
1636
1651
|
* @param {boolean} [all=true] - replace all matches
|
|
1637
|
-
* @param {boolean} [
|
|
1652
|
+
* @param {boolean} [javaMode=false] - activate java mode (different behaviour for capture groups and special characters)
|
|
1638
1653
|
* @returns {string}
|
|
1639
1654
|
* @private
|
|
1640
1655
|
*/
|
|
1641
|
-
replace(replacement, all = true,
|
|
1656
|
+
replace(replacement, all = true, javaMode = false) {
|
|
1642
1657
|
let res = '';
|
|
1643
1658
|
this.reset();
|
|
1644
1659
|
while (this.find()) {
|
|
1645
|
-
res += this.appendReplacement(replacement,
|
|
1660
|
+
res += this.appendReplacement(replacement, javaMode);
|
|
1646
1661
|
if (!all) {
|
|
1647
1662
|
break;
|
|
1648
1663
|
}
|
|
@@ -6199,6 +6214,20 @@ class RE2JS {
|
|
|
6199
6214
|
return Utils.quoteMeta(str);
|
|
6200
6215
|
}
|
|
6201
6216
|
|
|
6217
|
+
/**
|
|
6218
|
+
* Quotes '\' and '$' in {@code str}, so that the returned string could be used in
|
|
6219
|
+
* replacement methods as a literal replacement of {@code str}.
|
|
6220
|
+
*
|
|
6221
|
+
* This is a convenience delegation to {@link Matcher.quoteReplacement}.
|
|
6222
|
+
*
|
|
6223
|
+
* @param {string} str the string to be quoted
|
|
6224
|
+
* @param {boolean} [javaMode=false] whether the replacement will be used in javaMode
|
|
6225
|
+
* @returns {string} the quoted string
|
|
6226
|
+
*/
|
|
6227
|
+
static quoteReplacement(str, javaMode = false) {
|
|
6228
|
+
return Matcher.quoteReplacement(str, javaMode);
|
|
6229
|
+
}
|
|
6230
|
+
|
|
6202
6231
|
/**
|
|
6203
6232
|
* Translates a given regular expression string to ensure compatibility with RE2JS.
|
|
6204
6233
|
*
|