jquery.inputmask.regex.extensions.js 8.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153
  1. /*
  2. Input Mask plugin extensions
  3. http://github.com/RobinHerbots/jquery.inputmask
  4. Copyright (c) 2010 - 2013 Robin Herbots
  5. Licensed under the MIT license (http://www.opensource.org/licenses/mit-license.php)
  6. Version: 0.0.0
  7. Regex extensions on the jquery.inputmask base
  8. Allows for using regular expressions as a mask
  9. */
  10. (function ($) {
  11. $.extend($.inputmask.defaults.aliases, { // $(selector).inputmask("Regex", { regex: "[0-9]*"}
  12. 'Regex': {
  13. mask: "r",
  14. greedy: false,
  15. repeat: "*",
  16. regex: null,
  17. regexTokens: null,
  18. //Thx to https://github.com/slevithan/regex-colorizer for the tokenizer regex
  19. 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,
  20. quantifierFilter: /[0-9]+[^,]/,
  21. definitions: {
  22. 'r': {
  23. validator: function (chrs, buffer, pos, strict, opts) {
  24. function regexToken() {
  25. this.matches = [];
  26. this.isGroup = false;
  27. this.isQuantifier = false;
  28. }
  29. function analyseRegex() {
  30. var currentToken = new regexToken(), match, m, opengroups = [];
  31. opts.regexTokens = [];
  32. // The tokenizer regex does most of the tokenization grunt work
  33. while (match = opts.tokenizer.exec(opts.regex)) {
  34. m = match[0];
  35. switch (m.charAt(0)) {
  36. case "[": // Character class
  37. case "\\": // Escape or backreference
  38. if (currentToken["isGroup"] !== true) {
  39. currentToken = new regexToken();
  40. opts.regexTokens.push(currentToken);
  41. }
  42. if (opengroups.length > 0) {
  43. opengroups[opengroups.length - 1]["matches"].push(m);
  44. } else {
  45. currentToken.matches.push(m);
  46. }
  47. break;
  48. case "(": // Group opening
  49. currentToken = new regexToken();
  50. currentToken.isGroup = true;
  51. opengroups.push(currentToken);
  52. break;
  53. case ")": // Group closing
  54. var groupToken = opengroups.pop();
  55. if (opengroups.length > 0) {
  56. opengroups[opengroups.length - 1]["matches"].push(groupToken);
  57. } else {
  58. opts.regexTokens.push(groupToken);
  59. currentToken = new regexToken();
  60. opts.regexTokens.push(currentToken);
  61. }
  62. break;
  63. case "{": //Quantifier
  64. var quantifier = new regexToken();
  65. quantifier.isQuantifier = true;
  66. quantifier.matches.push(m);
  67. if (opengroups.length > 0) {
  68. opengroups[opengroups.length - 1]["matches"].push(quantifier);
  69. } else {
  70. currentToken.matches.push(quantifier);
  71. }
  72. break;
  73. default:
  74. // Vertical bar (alternator)
  75. // ^ or $ anchor
  76. // Dot (.)
  77. // Literal character sequence
  78. if (opengroups.length > 0) {
  79. opengroups[opengroups.length - 1]["matches"].push(m);
  80. } else {
  81. currentToken.matches.push(m);
  82. }
  83. }
  84. }
  85. };
  86. function validateRegexToken(token, fromGroup) {
  87. var isvalid = false;
  88. if (fromGroup) {
  89. regexPart += "(";
  90. openGroupCount++;
  91. }
  92. for (var mndx = 0; mndx < token["matches"].length; mndx++) {
  93. var matchToken = token["matches"][mndx];
  94. if (matchToken["isGroup"] == true) {
  95. isvalid = validateRegexToken(matchToken, true);
  96. } else if (matchToken["isQuantifier"] == true) {
  97. matchToken = matchToken["matches"][0];
  98. var quantifierMax = opts.quantifierFilter.exec(matchToken)[0].replace("}", "");
  99. var testExp = regexPart + "{1," + quantifierMax + "}"; //relax quantifier validation
  100. for (var j = 0; j < openGroupCount; j++) {
  101. testExp += ")";
  102. }
  103. var exp = new RegExp("^(" + testExp + ")$");
  104. isvalid = exp.test(bufferStr);
  105. regexPart += matchToken;
  106. }
  107. else {
  108. regexPart += matchToken;
  109. var testExp = regexPart.replace(/\|$/, "");
  110. for (var j = 0; j < openGroupCount; j++) {
  111. testExp += ")";
  112. }
  113. var exp = new RegExp("^(" + testExp + ")$");
  114. isvalid = exp.test(bufferStr);
  115. //console.log(bufferStr + " " + exp + " " + isvalid);
  116. }
  117. if (isvalid) break;
  118. }
  119. if (fromGroup) {
  120. regexPart += ")";
  121. openGroupCount--;
  122. }
  123. return isvalid;
  124. }
  125. if (opts.regexTokens == null) {
  126. analyseRegex();
  127. }
  128. var cbuffer = buffer.slice(), regexPart = "", isValid = false, openGroupCount = 0;
  129. cbuffer.splice(pos, 0, chrs);
  130. var bufferStr = cbuffer.join('');
  131. for (var i = 0; i < opts.regexTokens.length; i++) {
  132. var regexToken = opts.regexTokens[i];
  133. isValid = validateRegexToken(regexToken, regexToken["isGroup"]);
  134. if (isValid) break;
  135. }
  136. return isValid;
  137. },
  138. cardinality: 1
  139. }
  140. }
  141. }
  142. });
  143. })(jQuery);