Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Also update .lastIndex on natively sticky regexps #161

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion src/xregexp.js
Original file line number Diff line number Diff line change
Expand Up @@ -857,7 +857,7 @@ XRegExp.exec = function(str, regex, pos, sticky) {
match = null;
}

if (regex.global) {
if (regex.global || regex.sticky && sticky !== false) {
regex.lastIndex = match ? r2.lastIndex : 0;
}

Expand Down
56 changes: 55 additions & 1 deletion tests/spec/s-xregexp-methods.js
Original file line number Diff line number Diff line change
Expand Up @@ -260,7 +260,7 @@ describe('XRegExp.exec()', function() {
expect(XRegExp.exec('abcxdef', /(a)/)).toEqualMatch(['a', 'a']);
});

it('should not modify the lastIndex of a nonglobal regex', function() {
it('should not modify the lastIndex of a nonglobal, non-sticky regex', function() {
var regexX = /x/;
var str = 'abcxdef';
XRegExp.exec(str, regexX);
Expand Down Expand Up @@ -307,6 +307,60 @@ describe('XRegExp.exec()', function() {
expect(regex.lastIndex).toBe(0);
});

if (hasNativeY) {
it('should not modify the lastIndex of a sticky regex when exec is invoked in explicitly non-sticky mode', function() {
var regexX = new RegExp('x', 'y');
var str = 'abcxdef';
XRegExp.exec(str, regexX, 0, false);

expect(regexX.lastIndex).toBe(0);

regexX.lastIndex = 5;
XRegExp.exec(str, regexX, 0, false);

expect(regexX.lastIndex).toBe(5);

var regexZ = new RegExp('z', 'y');
regexZ.lastIndex = 5;
XRegExp.exec(str, regexZ, 0, false);

expect(regexZ.lastIndex).toBe(5);
});

it('should set the lastIndex of a sticky regex to the end position of a successful match', function() {
var regex1 = new RegExp('x', 'y');
XRegExp.exec('abcxdef', regex1, 3);

expect(regex1.lastIndex).toBe(4);

var regex2 = new RegExp('x', 'y');
XRegExp.exec('abcxdef', regex2, 3, 'sticky');

expect(regex2.lastIndex).toBe(4);
});

it('should set the lastIndex of a sticky regex to 0 after a failed match', function() {
var regexZ = new RegExp('z', 'y');
regexZ.lastIndex = 1;
XRegExp.exec('abcxdef', regexZ);

expect(regexZ.lastIndex).toBe(0);

var regexX = new RegExp('x', 'y');
regexX.lastIndex = 1;
XRegExp.exec('abcxdef', regexX, 2, 'sticky');

expect(regexX.lastIndex).toBe(0);
});

it('should not increment the lastIndex of a sticky regex after a zero-length match', function() {
var regex = new RegExp('', 'y');
XRegExp.exec('abc', regex);

expect(regex.lastIndex).toBe(0);
});
}

it('should convert any nonstring subject to a string', function() {
var values = [
undefined,
Expand Down
2 changes: 1 addition & 1 deletion xregexp-all.js
Original file line number Diff line number Diff line change
Expand Up @@ -3503,7 +3503,7 @@ XRegExp.exec = function(str, regex, pos, sticky) {
match = null;
}

if (regex.global) {
if (regex.global || regex.sticky && sticky !== false) {
regex.lastIndex = match ? r2.lastIndex : 0;
}

Expand Down