regex.js (2666B)
1var match1 = /\d/, // 0 - 9 2 match2 = /\d\d/, // 00 - 99 3 match3 = /\d{3}/, // 000 - 999 4 match4 = /\d{4}/, // 0000 - 9999 5 match6 = /[+-]?\d{6}/, // -999999 - 999999 6 match1to2 = /\d\d?/, // 0 - 99 7 match3to4 = /\d\d\d\d?/, // 999 - 9999 8 match5to6 = /\d\d\d\d\d\d?/, // 99999 - 999999 9 match1to3 = /\d{1,3}/, // 0 - 999 10 match1to4 = /\d{1,4}/, // 0 - 9999 11 match1to6 = /[+-]?\d{1,6}/, // -999999 - 999999 12 matchUnsigned = /\d+/, // 0 - inf 13 matchSigned = /[+-]?\d+/, // -inf - inf 14 matchOffset = /Z|[+-]\d\d:?\d\d/gi, // +00:00 -00:00 +0000 -0000 or Z 15 matchShortOffset = /Z|[+-]\d\d(?::?\d\d)?/gi, // +00 -00 +00:00 -00:00 +0000 -0000 or Z 16 matchTimestamp = /[+-]?\d+(\.\d{1,3})?/, // 123456789 123456789.123 17 // any word (or two) characters or numbers including two/three word month in arabic. 18 // includes scottish gaelic two word and hyphenated months 19 matchWord = 20 /[0-9]{0,256}['a-z\u00A0-\u05FF\u0700-\uD7FF\uF900-\uFDCF\uFDF0-\uFF07\uFF10-\uFFEF]{1,256}|[\u0600-\u06FF\/]{1,256}(\s*?[\u0600-\u06FF]{1,256}){1,2}/i, 21 match1to2NoLeadingZero = /^[1-9]\d?/, // 1-99 22 match1to2HasZero = /^([1-9]\d|\d)/, // 0-99 23 regexes; 24 25export { 26 match1, 27 match2, 28 match3, 29 match4, 30 match6, 31 match1to2, 32 match3to4, 33 match5to6, 34 match1to3, 35 match1to4, 36 match1to6, 37 matchUnsigned, 38 matchSigned, 39 matchOffset, 40 matchShortOffset, 41 matchTimestamp, 42 matchWord, 43 match1to2NoLeadingZero, 44 match1to2HasZero, 45}; 46 47import hasOwnProp from '../utils/has-own-prop'; 48import isFunction from '../utils/is-function'; 49 50regexes = {}; 51 52export function addRegexToken(token, regex, strictRegex) { 53 regexes[token] = isFunction(regex) 54 ? regex 55 : function (isStrict, localeData) { 56 return isStrict && strictRegex ? strictRegex : regex; 57 }; 58} 59 60export function getParseRegexForToken(token, config) { 61 if (!hasOwnProp(regexes, token)) { 62 return new RegExp(unescapeFormat(token)); 63 } 64 65 return regexes[token](config._strict, config._locale); 66} 67 68// Code from http://stackoverflow.com/questions/3561493/is-there-a-regexp-escape-function-in-javascript 69function unescapeFormat(s) { 70 return regexEscape( 71 s 72 .replace('\\', '') 73 .replace( 74 /\\(\[)|\\(\])|\[([^\]\[]*)\]|\\(.)/g, 75 function (matched, p1, p2, p3, p4) { 76 return p1 || p2 || p3 || p4; 77 } 78 ) 79 ); 80} 81 82export function regexEscape(s) { 83 return s.replace(/[-\/\\^$*+?.()|[\]{}]/g, '\\$&'); 84}