Skip to content

Commit 7188a61

Browse files
Reset lastIndex on RegExp objects that get reused in closures
Signed-off-by: andrew-coleman <andrew_coleman@uk.ibm.com>
1 parent daaa03e commit 7188a61

File tree

3 files changed

+59
-2
lines changed

3 files changed

+59
-2
lines changed

src/jsonata.js

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1089,8 +1089,9 @@ var jsonata = (function() {
10891089
*/
10901090
function evaluateRegex(expr) {
10911091
var re = new RegExp(expr.value);
1092-
var closure = function(str) {
1092+
var closure = function(str, fromIndex) {
10931093
var result;
1094+
re.lastIndex = fromIndex || 0;
10941095
var match = re.exec(str);
10951096
if(match !== null) {
10961097
result = {
@@ -1108,7 +1109,7 @@ var jsonata = (function() {
11081109
if(re.lastIndex >= str.length) {
11091110
return undefined;
11101111
} else {
1111-
var next = closure(str);
1112+
var next = closure(str, re.lastIndex);
11121113
if(next && next.match === '') {
11131114
// matches zero length string; this will never progress
11141115
throw {
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
{
2+
"expr": "$map($, $match(?, /^(\\w*\\s\\w*)/)).match",
3+
"data": [
4+
"Felicia Saunders",
5+
"Jimmy Schultz",
6+
"Dolores Figueroa",
7+
"Craig Moreno",
8+
"Lindsey Hall",
9+
"Bonnie Russell",
10+
"Kristin Stewart",
11+
"Owen Reid",
12+
"Brenda Sherman",
13+
"Dwayne Baldwin"
14+
],
15+
"bindings": {},
16+
"result": [
17+
"Felicia Saunders",
18+
"Jimmy Schultz",
19+
"Dolores Figueroa",
20+
"Craig Moreno",
21+
"Lindsey Hall",
22+
"Bonnie Russell",
23+
"Kristin Stewart",
24+
"Owen Reid",
25+
"Brenda Sherman",
26+
"Dwayne Baldwin"
27+
]
28+
}
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
{
2+
"expr": "$map($, function($i){ $match($i, /^(\\w*\\s\\w*)/) }).match",
3+
"data": [
4+
"Felicia Saunders",
5+
"Jimmy Schultz",
6+
"Dolores Figueroa",
7+
"Craig Moreno",
8+
"Lindsey Hall",
9+
"Bonnie Russell",
10+
"Kristin Stewart",
11+
"Owen Reid",
12+
"Brenda Sherman",
13+
"Dwayne Baldwin"
14+
],
15+
"bindings": {},
16+
"result": [
17+
"Felicia Saunders",
18+
"Jimmy Schultz",
19+
"Dolores Figueroa",
20+
"Craig Moreno",
21+
"Lindsey Hall",
22+
"Bonnie Russell",
23+
"Kristin Stewart",
24+
"Owen Reid",
25+
"Brenda Sherman",
26+
"Dwayne Baldwin"
27+
]
28+
}

0 commit comments

Comments
 (0)