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:
parent
ef014fd054
commit
373ff54384
|
@ -121,6 +121,21 @@ func TestRegExp_exec(t *testing.T) {
|
|||
[ ghi, lastIndex ];
|
||||
`, "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(`
|
||||
var abc = /[abc](\d)?/.exec("a0 b c1 d3");
|
||||
[ abc.length, abc.input, abc.index, abc ];
|
||||
|
|
|
@ -108,9 +108,11 @@ func execRegExp(this *_object, target string) (match bool, result []int) {
|
|||
endIndex := int(lastIndex) + result[1]
|
||||
// We do this shift here because the .FindStringSubmatchIndex above
|
||||
// was done on a local subordinate slice of the string, not the whole string
|
||||
for index, _ := range result {
|
||||
for index, offset := range result {
|
||||
if offset != -1 {
|
||||
result[index] += int(startIndex)
|
||||
}
|
||||
}
|
||||
if global {
|
||||
//this.defineProperty("lastIndex", toValue_(endIndex), 0111, true)
|
||||
this.put("lastIndex", toValue_int(endIndex), true)
|
||||
|
|
Loading…
Reference in New Issue
Block a user