@@ -15,21 +15,30 @@ var SharpRegex = /** @class */ (function () {
15
15
* @param {string } subject The subject string
16
16
*/
17
17
SharpRegex . prototype . getFullDetails = function ( subject ) {
18
- var matches , firstIndex , indexMapper = Object . assign ( { 0 : 0 } , this . groupIndexMapper ) , previousGroups = Object . assign ( { 0 : [ ] } , this . previousGroupsForGroup ) , regexClone = this . regexp , result = [ ] ;
19
- while ( ( matches = regexClone . exec ( subject ) ) !== null ) {
20
- firstIndex = matches . index ;
21
- result . push ( Object . keys ( indexMapper ) . map ( function ( group ) {
22
- var mapped = indexMapper [ group ] , start = firstIndex + previousGroups [ group ] . reduce ( function ( sum , i ) { return sum + ( matches [ i ] ? matches [ i ] . length : 0 ) ; } , 0 ) ;
23
- return {
24
- match : matches [ mapped ] ,
25
- start : start ,
26
- end : start + ( matches [ mapped ] ? matches [ mapped ] . length : 0 ) ,
27
- group : parseInt ( group )
28
- } ;
29
- } ) ) ;
30
- if ( regexClone . lastIndex == matches . index ) {
18
+ var matches , indexMapper = Object . assign ( { 0 : 0 } , this . groupIndexMapper ) , previousGroups = Object . assign ( { 0 : [ ] } , this . previousGroupsForGroup ) , regexClone = this . regexp , result = [ ] ;
19
+ var _loop_1 = function ( ) {
20
+ var firstIndex = matches . index , localResult = [ ] ;
21
+ Object . keys ( indexMapper ) . forEach ( function ( group ) {
22
+ var mapped = indexMapper [ group ] , start = firstIndex + previousGroups [ group ] . reduce ( function ( sum , i ) { return sum + ( matches [ i ] ? matches [ i ] . length : 0 ) ; } , 0 ) , end = start + ( matches [ mapped ] ? matches [ mapped ] . length : 0 ) ;
23
+ if ( group === "0" || end > start ) {
24
+ localResult . push ( {
25
+ match : matches [ mapped ] ,
26
+ start : start ,
27
+ end : end ,
28
+ group : parseInt ( group )
29
+ } ) ;
30
+ }
31
+ } ) ;
32
+ result . push ( localResult ) ;
33
+ /**
34
+ * Prevent from infinite loop
35
+ */
36
+ if ( regexClone . lastIndex == firstIndex ) {
31
37
regexClone . lastIndex ++ ;
32
38
}
39
+ } ;
40
+ while ( ( matches = regexClone . exec ( subject ) ) !== null ) {
41
+ _loop_1 ( ) ;
33
42
}
34
43
return result ;
35
44
} ;
@@ -38,21 +47,30 @@ var SharpRegex = /** @class */ (function () {
38
47
* @param {string } subject The subject string
39
48
*/
40
49
SharpRegex . prototype . getGroupsDetails = function ( subject ) {
41
- var matches = this . regexp . exec ( subject ) , indexMapper = this . groupIndexMapper , previousGroups = this . previousGroupsForGroup , firstIndex , regexClone = this . regexp , result = [ ] ;
42
- while ( ( matches = regexClone . exec ( subject ) ) !== null ) {
43
- firstIndex = matches . index ;
44
- result . push ( Object . keys ( indexMapper ) . map ( function ( group ) {
45
- var mapped = indexMapper [ group ] , start = firstIndex + previousGroups [ group ] . reduce ( function ( sum , i ) { return sum + ( matches [ i ] ? matches [ i ] . length : 0 ) ; } , 0 ) ;
46
- return {
47
- match : matches [ mapped ] ,
48
- start : start ,
49
- end : start + ( matches [ mapped ] ? matches [ mapped ] . length : 0 ) ,
50
- group : parseInt ( group )
51
- } ;
52
- } ) ) ;
53
- if ( regexClone . lastIndex == matches . index ) {
50
+ var matches = this . regexp . exec ( subject ) , indexMapper = this . groupIndexMapper , previousGroups = this . previousGroupsForGroup , regexClone = this . regexp , result = [ ] ;
51
+ var _loop_2 = function ( ) {
52
+ var firstIndex = matches . index , localResult = [ ] ;
53
+ Object . keys ( indexMapper ) . forEach ( function ( group ) {
54
+ var mapped = indexMapper [ group ] , start = firstIndex + previousGroups [ group ] . reduce ( function ( sum , i ) { return sum + ( matches [ i ] ? matches [ i ] . length : 0 ) ; } , 0 ) , end = start + ( matches [ mapped ] ? matches [ mapped ] . length : 0 ) ;
55
+ if ( group === "0" || end > start ) {
56
+ localResult . push ( {
57
+ match : matches [ mapped ] ,
58
+ start : start ,
59
+ end : start + ( matches [ mapped ] ? matches [ mapped ] . length : 0 ) ,
60
+ group : parseInt ( group )
61
+ } ) ;
62
+ }
63
+ } ) ;
64
+ result . push ( localResult ) ;
65
+ /**
66
+ * Prevent from infinite loop
67
+ */
68
+ if ( regexClone . lastIndex == firstIndex ) {
54
69
regexClone . lastIndex ++ ;
55
70
}
71
+ } ;
72
+ while ( ( matches = regexClone . exec ( subject ) ) !== null ) {
73
+ _loop_2 ( ) ;
56
74
}
57
75
return result ;
58
76
} ;
@@ -73,7 +91,10 @@ var SharpRegex = /** @class */ (function () {
73
91
start : startIndex ,
74
92
end : endIndex ,
75
93
} ) ;
76
- if ( regexClone . lastIndex == matches . index ) {
94
+ /**
95
+ * Prevent from infinite loop
96
+ */
97
+ if ( regexClone . lastIndex == firstIndex ) {
77
98
regexClone . lastIndex ++ ;
78
99
}
79
100
}
@@ -108,15 +129,15 @@ var SharpRegex = /** @class */ (function () {
108
129
*/
109
130
SharpRegex . prototype . _fillGroups = function ( regex ) {
110
131
var regexString = regex . source , modifier = regex . flags , tester = / ( \\ \( ) | ( \\ \) ) | ( \( \? ) | ( \( ) | ( \) (?: { \d + , ? \d * } | [ * + ? ] ) ? \? ? ) / g, modifiedRegex = regexString , lastGroupStartPosition = - 1 , lastGroupEndPosition = - 1 , lastNonGroupStartPosition = - 1 , lastNonGroupEndPosition = - 1 , groupsAdded = 0 , groupCount = 0 , matchArr , nonGroupPositions = [ ] , groupPositions = [ ] , groupNumber = [ ] , currentLengthIndexes = [ ] , groupIndexMapper = { } , previousGroupsForGroup = { } ;
111
- var _loop_1 = function ( ) {
112
- if ( matchArr [ 1 ] || matchArr [ 2 ] ) {
132
+ var _loop_3 = function ( ) {
133
+ if ( matchArr [ 1 ] || matchArr [ 2 ] ) { // ignore escaped brackets \(, \)
113
134
}
114
- if ( matchArr [ 3 ] ) {
135
+ if ( matchArr [ 3 ] ) { // non capturing group (?
115
136
var index = matchArr . index + matchArr [ 0 ] . length - 1 ;
116
137
lastNonGroupStartPosition = index ;
117
138
nonGroupPositions . push ( index ) ;
118
139
}
119
- else if ( matchArr [ 4 ] ) {
140
+ else if ( matchArr [ 4 ] ) { // capturing group (
120
141
var index = matchArr . index + matchArr [ 0 ] . length - 1 , lastGroupPosition = Math . max ( lastGroupStartPosition , lastGroupEndPosition ) ;
121
142
// if a (? is found add ) before it
122
143
if ( lastNonGroupStartPosition > lastGroupPosition ) {
@@ -163,7 +184,7 @@ var SharpRegex = /** @class */ (function () {
163
184
groupIndexMapper [ groupCount ] = groupCount + groupsAdded ;
164
185
previousGroupsForGroup [ groupCount ] = currentLengthIndexes . slice ( ) ;
165
186
}
166
- else if ( matchArr [ 5 ] ) {
187
+ else if ( matchArr [ 5 ] ) { // closing bracket ), )+, )+?, ){1,}?, ){1,1111}?
167
188
var index = matchArr . index + matchArr [ 0 ] . length - 1 ;
168
189
if ( ( groupPositions . length && ! nonGroupPositions . length ) ||
169
190
groupPositions [ groupPositions . length - 1 ] > nonGroupPositions [ nonGroupPositions . length - 1 ] ) {
@@ -187,7 +208,7 @@ var SharpRegex = /** @class */ (function () {
187
208
} ;
188
209
var this_1 = this ;
189
210
while ( ( matchArr = tester . exec ( regexString ) ) !== null ) {
190
- _loop_1 ( ) ;
211
+ _loop_3 ( ) ;
191
212
}
192
213
return { regexp : new RegExp ( modifiedRegex , modifier ) , groupIndexMapper : groupIndexMapper , previousGroupsForGroup : previousGroupsForGroup } ;
193
214
} ;
0 commit comments