Skip to content
This repository was archived by the owner on Apr 12, 2024. It is now read-only.

Commit e25ed0d

Browse files
shahatapetebacondarwin
authored andcommitted
fix(angular.copy): clone regexp flags correctly
Closes #5781 Closes #8337
1 parent cd6d21e commit e25ed0d

File tree

2 files changed

+16
-1
lines changed

2 files changed

+16
-1
lines changed

src/Angular.js

+2-1
Original file line numberDiff line numberDiff line change
@@ -786,7 +786,8 @@ function copy(source, destination, stackSource, stackDest) {
786786
} else if (isDate(source)) {
787787
destination = new Date(source.getTime());
788788
} else if (isRegExp(source)) {
789-
destination = new RegExp(source.source);
789+
destination = new RegExp(source.source, source.toString().match(/[^\/]*$/)[0]);
790+
destination.lastIndex = source.lastIndex;
790791
} else if (isObject(source)) {
791792
destination = copy(source, {}, stackSource, stackDest);
792793
}

test/AngularSpec.js

+14
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,20 @@ describe('angular', function() {
4545
expect(copy(re) === re).toBeFalsy();
4646
});
4747

48+
it("should copy RegExp with flags", function() {
49+
var re = new RegExp('.*', 'gim');
50+
expect(copy(re).global).toBe(true);
51+
expect(copy(re).ignoreCase).toBe(true);
52+
expect(copy(re).multiline).toBe(true);
53+
});
54+
55+
it("should copy RegExp with lastIndex", function() {
56+
var re = /a+b+/g;
57+
var str = 'ab aabb';
58+
expect(re.exec(str)[0]).toEqual('ab');
59+
expect(copy(re).exec(str)[0]).toEqual('aabb');
60+
});
61+
4862
it("should deeply copy literal RegExp", function() {
4963
var objWithRegExp = {
5064
re: /.*/

0 commit comments

Comments
 (0)