]> git.ipfire.org Git - thirdparty/moment.git/commitdiff
feat(#getParseRegexForToken): escape special RegExp characters to increase presicion... 1094/head
authorTimur Kozmenko <timraell@gmail.com>
Sun, 15 Sep 2013 11:10:04 +0000 (18:10 +0700)
committerTimur Kozmenko <timraell@gmail.com>
Sun, 15 Sep 2013 11:10:04 +0000 (18:10 +0700)
moment.js
test/moment/create.js

index e0792671daee644e4a65edc9f40f2c22964eadb5..1a10cfbd82b448f245f577d7991d10bacee55312 100644 (file)
--- a/moment.js
+++ b/moment.js
         return units ? unitAliases[units] || units.toLowerCase().replace(/(.)s$/, '$1') : units;
     }
 
+    function regexpEscape(text) {
+        return text.replace(/[-[\]{}()*+?.,\\^$|#\s]/g, "\\$&");
+    }
 
     /************************************
         Languages
         case 's':
             return parseTokenOneOrTwoDigits;
         default :
-            return new RegExp(token.replace('\\', ''));
+            return new RegExp(regexpEscape(token.replace('\\', '')));
         }
     }
 
         tokens = expandFormat(config._f, lang).match(formattingTokens);
 
         config._a = [];
-
         for (i = 0; i < tokens.length; i++) {
             parsedInput = (getParseRegexForToken(tokens[i], config).exec(string) || [])[0];
             if (parsedInput) {
index 87bdb77ea490437ab578410baba0728ff3a7ac50..02742660e64c6567304c42ef346dae2c5f208589 100644 (file)
@@ -293,7 +293,7 @@ exports.create = {
     },
 
     "string with array of formats" : function (test) {
-        test.expect(15);
+        test.expect(16);
 
         test.equal(moment('11-02-1999', ['MM-DD-YYYY', 'DD-MM-YYYY']).format('MM DD YYYY'), '11 02 1999', 'switching month and day');
         test.equal(moment('02-11-1999', ['MM/DD/YYYY', 'YYYY MM DD', 'MM-DD-YYYY']).format('MM DD YYYY'), '02 11 1999', 'year last');
@@ -310,6 +310,7 @@ exports.create = {
         test.equal(moment('13-14-1999', ['DD/MM/YYYY', 'MM/DD/YYYY']).format('MM DD YYYY'), '02 13 2000', 'either can be a month, day first format');
 
         test.equal(moment('11-02-10', ['MM/DD/YY', 'YY MM DD', 'DD-MM-YY']).format('MM DD YYYY'), '02 11 2010', 'all unparsed substrings have influence on format penalty');
+        test.equal(moment('11-02-10', ['MM.DD.YY', 'DD-MM-YY']).format('MM DD YYYY'), '02 11 2010', 'escape RegExp special characters on comparing');
 
         test.equal(moment('13-14-98', ['DD MM YY', 'DD MM YYYY'])._f, 'DD MM YY', 'use two digit year');
         test.equal(moment('13-14-1998', ['DD MM YY', 'DD MM YYYY'])._f, 'DD MM YYYY', 'use four digit year');