1
0
mirror of https://github.com/robertkrimen/otto synced 2025-10-12 20:27:30 +08:00

fix: Incorrect groups offsets if lastIndex is set (#393)

This commit is contained in:
Luka Zakrajšek 2021-06-14 20:17:06 +02:00 committed by GitHub
parent ef014fd054
commit 373ff54384
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 19 additions and 2 deletions

View File

@ -121,6 +121,21 @@ func TestRegExp_exec(t *testing.T) {
[ ghi, lastIndex ]; [ ghi, lastIndex ];
`, "3,7") `, "3,7")
test(`
abc = /(\d)?(s)/g;
def = 's';
ghi = abc.exec(def);
[ ghi[1] === undefined, ghi[2] === 's' ];
`, "true,true")
test(`
abc = /(\d)?(s)/g;
def = '%s';
abc.lastIndex = 1;
ghi = abc.exec(def);
[ ghi[1] === undefined, ghi[2] === 's' ];
`, "true,true")
test(` test(`
var abc = /[abc](\d)?/.exec("a0 b c1 d3"); var abc = /[abc](\d)?/.exec("a0 b c1 d3");
[ abc.length, abc.input, abc.index, abc ]; [ abc.length, abc.input, abc.index, abc ];

View File

@ -108,8 +108,10 @@ func execRegExp(this *_object, target string) (match bool, result []int) {
endIndex := int(lastIndex) + result[1] endIndex := int(lastIndex) + result[1]
// We do this shift here because the .FindStringSubmatchIndex above // We do this shift here because the .FindStringSubmatchIndex above
// was done on a local subordinate slice of the string, not the whole string // was done on a local subordinate slice of the string, not the whole string
for index, _ := range result { for index, offset := range result {
result[index] += int(startIndex) if offset != -1 {
result[index] += int(startIndex)
}
} }
if global { if global {
//this.defineProperty("lastIndex", toValue_(endIndex), 0111, true) //this.defineProperty("lastIndex", toValue_(endIndex), 0111, true)