index.js 694 B

123456789101112131415161718192021222324252627282930
  1. "use strict";
  2. /**
  3. * RegexParser
  4. * Parses a string input.
  5. *
  6. * @name RegexParser
  7. * @function
  8. * @param {String} input The string input that should be parsed as regular
  9. * expression.
  10. * @return {RegExp} The parsed regular expression.
  11. */
  12. var RegexParser = module.exports = function (input) {
  13. // Validate input
  14. if (typeof input !== "string") {
  15. throw new Error("Invalid input. Input must be a string");
  16. }
  17. // Parse input
  18. var m = input.match(/(\/?)(.+)\1([a-z]*)/i);
  19. // Invalid flags
  20. if (m[3] && !/^(?!.*?(.).*?\1)[gmixXsuUAJ]+$/.test(m[3])) {
  21. return RegExp(input);
  22. }
  23. // Create the regular expression
  24. return new RegExp(m[2], m[3]);
  25. };