From cfc7b9d60e9e1cc4c37f103b930e0d0f01d37b2b Mon Sep 17 00:00:00 2001 From: Shahar Talmi Date: Fri, 25 Jul 2014 15:31:56 +0300 Subject: [PATCH] fix(angular.copy): clone regexp flags correctly Closes #5781 --- src/Angular.js | 3 ++- test/AngularSpec.js | 14 ++++++++++++++ 2 files changed, 16 insertions(+), 1 deletion(-) diff --git a/src/Angular.js b/src/Angular.js index 6de88113b708..3e9982a54aab 100644 --- a/src/Angular.js +++ b/src/Angular.js @@ -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]); + destination.lastIndex = source.lastIndex; } else if (isObject(source)) { var emptyObject = Object.create(Object.getPrototypeOf(source)); destination = copy(source, emptyObject, stackSource, stackDest); diff --git a/test/AngularSpec.js b/test/AngularSpec.js index 76a375735e64..540098353cf6 100644 --- a/test/AngularSpec.js +++ b/test/AngularSpec.js @@ -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: /.*/