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

Paste-bin is for source code and general debugging text.

Login or Register to edit, delete and keep track of your pastes and more.

Raw Paste

Login or Register to edit or fork this paste. It's free.