inputmask.regex.extensions.js 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110
  1. /*!
  2. * inputmask.regex.extensions.js
  3. * https://github.com/RobinHerbots/Inputmask
  4. * Copyright (c) 2010 - 2017 Robin Herbots
  5. * Licensed under the MIT license (http://www.opensource.org/licenses/mit-license.php)
  6. * Version: 3.3.10
  7. */
  8. !function(factory) {
  9. "function" == typeof define && define.amd ? define([ "./dependencyLibs/inputmask.dependencyLib", "./inputmask" ], factory) : "object" == typeof exports ? module.exports = factory(require("./dependencyLibs/inputmask.dependencyLib"), require("./inputmask")) : factory(window.dependencyLib || jQuery, window.Inputmask);
  10. }(function($, Inputmask) {
  11. return Inputmask.extendAliases({
  12. Regex: {
  13. mask: "r",
  14. greedy: !1,
  15. repeat: "*",
  16. regex: null,
  17. regexTokens: null,
  18. tokenizer: /\[\^?]?(?:[^\\\]]+|\\[\S\s]?)*]?|\\(?:0(?:[0-3][0-7]{0,2}|[4-7][0-7]?)?|[1-9][0-9]*|x[0-9A-Fa-f]{2}|u[0-9A-Fa-f]{4}|c[A-Za-z]|[\S\s]?)|\((?:\?[:=!]?)?|(?:[?*+]|\{[0-9]+(?:,[0-9]*)?\})\??|[^.?*+^${[()|\\]+|./g,
  19. quantifierFilter: /[0-9]+[^,]/,
  20. isComplete: function(buffer, opts) {
  21. return new RegExp(opts.regex, opts.casing ? "i" : "").test(buffer.join(""));
  22. },
  23. definitions: {
  24. r: {
  25. validator: function(chrs, maskset, pos, strict, opts) {
  26. function RegexToken(isGroup, isQuantifier) {
  27. this.matches = [], this.isGroup = isGroup || !1, this.isQuantifier = isQuantifier || !1,
  28. this.quantifier = {
  29. min: 1,
  30. max: 1
  31. }, this.repeaterPart = void 0;
  32. }
  33. function validateRegexToken(token, fromGroup) {
  34. var isvalid = !1;
  35. fromGroup && (regexPart += "(", openGroupCount++);
  36. for (var mndx = 0; mndx < token.matches.length; mndx++) {
  37. var matchToken = token.matches[mndx];
  38. if (!0 === matchToken.isGroup) isvalid = validateRegexToken(matchToken, !0); else if (!0 === matchToken.isQuantifier) {
  39. var crrntndx = $.inArray(matchToken, token.matches), matchGroup = token.matches[crrntndx - 1], regexPartBak = regexPart;
  40. if (isNaN(matchToken.quantifier.max)) {
  41. for (;matchToken.repeaterPart && matchToken.repeaterPart !== regexPart && matchToken.repeaterPart.length > regexPart.length && !(isvalid = validateRegexToken(matchGroup, !0)); ) ;
  42. (isvalid = isvalid || validateRegexToken(matchGroup, !0)) && (matchToken.repeaterPart = regexPart),
  43. regexPart = regexPartBak + matchToken.quantifier.max;
  44. } else {
  45. for (var i = 0, qm = matchToken.quantifier.max - 1; i < qm && !(isvalid = validateRegexToken(matchGroup, !0)); i++) ;
  46. regexPart = regexPartBak + "{" + matchToken.quantifier.min + "," + matchToken.quantifier.max + "}";
  47. }
  48. } else if (void 0 !== matchToken.matches) for (var k = 0; k < matchToken.length && !(isvalid = validateRegexToken(matchToken[k], fromGroup)); k++) ; else {
  49. var testExp;
  50. if ("[" == matchToken.charAt(0)) {
  51. testExp = regexPart, testExp += matchToken;
  52. for (j = 0; j < openGroupCount; j++) testExp += ")";
  53. isvalid = (exp = new RegExp("^(" + testExp + ")$", opts.casing ? "i" : "")).test(bufferStr);
  54. } else for (var l = 0, tl = matchToken.length; l < tl; l++) if ("\\" !== matchToken.charAt(l)) {
  55. testExp = regexPart, testExp = (testExp += matchToken.substr(0, l + 1)).replace(/\|$/, "");
  56. for (var j = 0; j < openGroupCount; j++) testExp += ")";
  57. var exp = new RegExp("^(" + testExp + ")$", opts.casing ? "i" : "");
  58. if (isvalid = exp.test(bufferStr)) break;
  59. }
  60. regexPart += matchToken;
  61. }
  62. if (isvalid) break;
  63. }
  64. return fromGroup && (regexPart += ")", openGroupCount--), isvalid;
  65. }
  66. var bufferStr, groupToken, cbuffer = maskset.buffer.slice(), regexPart = "", isValid = !1, openGroupCount = 0;
  67. null === opts.regexTokens && function() {
  68. var match, m, currentToken = new RegexToken(), opengroups = [];
  69. for (opts.regexTokens = []; match = opts.tokenizer.exec(opts.regex); ) switch ((m = match[0]).charAt(0)) {
  70. case "(":
  71. opengroups.push(new RegexToken(!0));
  72. break;
  73. case ")":
  74. groupToken = opengroups.pop(), opengroups.length > 0 ? opengroups[opengroups.length - 1].matches.push(groupToken) : currentToken.matches.push(groupToken);
  75. break;
  76. case "{":
  77. case "+":
  78. case "*":
  79. var quantifierToken = new RegexToken(!1, !0), mq = (m = m.replace(/[{}]/g, "")).split(","), mq0 = isNaN(mq[0]) ? mq[0] : parseInt(mq[0]), mq1 = 1 === mq.length ? mq0 : isNaN(mq[1]) ? mq[1] : parseInt(mq[1]);
  80. if (quantifierToken.quantifier = {
  81. min: mq0,
  82. max: mq1
  83. }, opengroups.length > 0) {
  84. var matches = opengroups[opengroups.length - 1].matches;
  85. (match = matches.pop()).isGroup || ((groupToken = new RegexToken(!0)).matches.push(match),
  86. match = groupToken), matches.push(match), matches.push(quantifierToken);
  87. } else (match = currentToken.matches.pop()).isGroup || ((groupToken = new RegexToken(!0)).matches.push(match),
  88. match = groupToken), currentToken.matches.push(match), currentToken.matches.push(quantifierToken);
  89. break;
  90. default:
  91. opengroups.length > 0 ? opengroups[opengroups.length - 1].matches.push(m) : currentToken.matches.push(m);
  92. }
  93. currentToken.matches.length > 0 && opts.regexTokens.push(currentToken);
  94. }(), cbuffer.splice(pos, 0, chrs), bufferStr = cbuffer.join("");
  95. for (var i = 0; i < opts.regexTokens.length; i++) {
  96. var regexToken = opts.regexTokens[i];
  97. if (isValid = validateRegexToken(regexToken, regexToken.isGroup)) break;
  98. }
  99. return isValid;
  100. },
  101. cardinality: 1
  102. }
  103. }
  104. }
  105. }), Inputmask;
  106. });