1417522d452258428d3658abf5aa3237c3d7b013edf0b1183bea85c19f988df02e058021ec97c94e70e0a75580a4157f770ff29d7427d5d7bf949fd3fa0e80 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384
  1. var 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. export {
  25. match1,
  26. match2,
  27. match3,
  28. match4,
  29. match6,
  30. match1to2,
  31. match3to4,
  32. match5to6,
  33. match1to3,
  34. match1to4,
  35. match1to6,
  36. matchUnsigned,
  37. matchSigned,
  38. matchOffset,
  39. matchShortOffset,
  40. matchTimestamp,
  41. matchWord,
  42. match1to2NoLeadingZero,
  43. match1to2HasZero,
  44. };
  45. import hasOwnProp from '../utils/has-own-prop';
  46. import isFunction from '../utils/is-function';
  47. regexes = {};
  48. export function addRegexToken(token, regex, strictRegex) {
  49. regexes[token] = isFunction(regex)
  50. ? regex
  51. : function (isStrict, localeData) {
  52. return isStrict && strictRegex ? strictRegex : regex;
  53. };
  54. }
  55. export function getParseRegexForToken(token, config) {
  56. if (!hasOwnProp(regexes, token)) {
  57. return new RegExp(unescapeFormat(token));
  58. }
  59. return regexes[token](config._strict, config._locale);
  60. }
  61. // Code from http://stackoverflow.com/questions/3561493/is-there-a-regexp-escape-function-in-javascript
  62. function unescapeFormat(s) {
  63. return regexEscape(
  64. s
  65. .replace('\\', '')
  66. .replace(
  67. /\\(\[)|\\(\])|\[([^\]\[]*)\]|\\(.)/g,
  68. function (matched, p1, p2, p3, p4) {
  69. return p1 || p2 || p3 || p4;
  70. }
  71. )
  72. );
  73. }
  74. export function regexEscape(s) {
  75. return s.replace(/[-\/\\^$*+?.()|[\]{}]/g, '\\$&');
  76. }