diff --git a/.eslintrc b/.eslintrc index 63ed51a7..d362d1d6 100644 --- a/.eslintrc +++ b/.eslintrc @@ -14,7 +14,7 @@ "max-statements": [1, 30], "new-cap": [2, { "capIsNewExceptions": ["ToInteger", "ToObject", "ToPrimitive", "ToUint32"] }], "no-constant-condition": [1], - "no-extend-native": [2, {"exceptions": ["Date", "Error"]}], + "no-extend-native": [2, {"exceptions": ["Date", "Error", "RegExp"]}], "no-extra-parens": [0], "no-extra-semi": [1], "no-func-assign": [1], diff --git a/es5-shim.js b/es5-shim.js index 143ae056..cb9cbdff 100644 --- a/es5-shim.js +++ b/es5-shim.js @@ -1768,4 +1768,22 @@ if (supportsDescriptors) { ensureNonEnumerable(Error.prototype, 'name'); } +if (String(/a/mig) !== '/a/gim') { + var regexToString = function toString() { + var str = '/' + this.source + '/'; + if (this.global) { + str += 'g'; + } + if (this.ignoreCase) { + str += 'i'; + } + if (this.multiline) { + str += 'm'; + } + return str; + }; + // can't use defineProperties here because of toString enumeration issue in IE <= 8 + RegExp.prototype.toString = regexToString; +} + })); diff --git a/tests/spec/s-regexp.js b/tests/spec/s-regexp.js index f8705c54..a336602b 100644 --- a/tests/spec/s-regexp.js +++ b/tests/spec/s-regexp.js @@ -6,20 +6,21 @@ describe('RegExp', function () { describe('#toString()', function () { describe('literals', function () { it('should return correct flags and in correct order', function () { - expect(/pattern/.toString()).toBe('/pattern/'); - expect(/pattern/i.toString()).toBe('/pattern/i'); - expect(/pattern/mi.toString()).toBe('/pattern/im'); - expect(/pattern/im.toString()).toBe('/pattern/im'); - expect(/pattern/mgi.toString()).toBe('/pattern/gim'); + expect(String(/pattern/)).toBe('/pattern/'); + expect(String(/pattern/i)).toBe('/pattern/i'); + expect(String(/pattern/mi)).toBe('/pattern/im'); + expect(String(/pattern/im)).toBe('/pattern/im'); + expect(String(/pattern/mgi)).toBe('/pattern/gim'); }); }); + describe('objects', function () { it('should return correct flags and in correct order', function () { - expect(new RegExp('pattern').toString()).toBe('/pattern/'); - expect(new RegExp('pattern', 'i').toString()).toBe('/pattern/i'); - expect(new RegExp('pattern', 'mi').toString()).toBe('/pattern/im'); - expect(new RegExp('pattern', 'im').toString()).toBe('/pattern/im'); - expect(new RegExp('pattern', 'mgi').toString()).toBe('/pattern/gim'); + expect(String(new RegExp('pattern'))).toBe('/pattern/'); + expect(String(new RegExp('pattern', 'i'))).toBe('/pattern/i'); + expect(String(new RegExp('pattern', 'mi'))).toBe('/pattern/im'); + expect(String(new RegExp('pattern', 'im'))).toBe('/pattern/im'); + expect(String(new RegExp('pattern', 'mgi'))).toBe('/pattern/gim'); }); }); });