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

fix(angular.copy): clone regexp flags correctly #8337

Closed
wants to merge 1 commit into from
Closed
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
3 changes: 2 additions & 1 deletion src/Angular.js
Original file line number Diff line number Diff line change
@@ -775,7 +775,8 @@ function copy(source, destination, stackSource, stackDest) {
} else if (isDate(source)) {
destination = new Date(source.getTime());
} else if (isRegExp(source)) {
destination = new RegExp(source.source);
destination = new RegExp(source.source, source.toString().match(/[^\/]*$/)[0]);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Now that is very neat. You're the first person I've seen do it that way.
Normally people just go through each of the flags in turn.

destination.lastIndex = source.lastIndex;
} else if (isObject(source)) {
var emptyObject = Object.create(Object.getPrototypeOf(source));
destination = copy(source, emptyObject, stackSource, stackDest);
14 changes: 14 additions & 0 deletions test/AngularSpec.js
Original file line number Diff line number Diff line change
@@ -55,6 +55,20 @@ describe('angular', function() {
expect(copy(re) === re).toBeFalsy();
});

it("should copy RegExp with flags", function() {
var re = new RegExp('.*', 'gim');
expect(copy(re).global).toBe(true);
expect(copy(re).ignoreCase).toBe(true);
expect(copy(re).multiline).toBe(true);
});

it("should copy RegExp with lastIndex", function() {
var re = /a+b+/g;
var str = 'ab aabb';
expect(re.exec(str)[0]).toEqual('ab');
expect(copy(re).exec(str)[0]).toEqual('aabb');
});

it("should deeply copy literal RegExp", function() {
var objWithRegExp = {
re: /.*/