You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

29 lines
694 B

4 years ago
  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. };