shExpMatch.js 1.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041
  1. "use strict";
  2. /**
  3. * Returns true if the string matches the specified shell
  4. * expression.
  5. *
  6. * Actually, currently the patterns are shell expressions,
  7. * not regular expressions.
  8. *
  9. * Examples:
  10. *
  11. * ``` js
  12. * shExpMatch("http://home.netscape.com/people/ari/index.html", "*\/ari/*")
  13. * // is true.
  14. *
  15. * shExpMatch("http://home.netscape.com/people/montulli/index.html", "*\/ari/*")
  16. * // is false.
  17. * ```
  18. *
  19. * @param {String} str is any string to compare (e.g. the URL, or the hostname).
  20. * @param {String} shexp is a shell expression to compare against.
  21. * @return {Boolean} true if the string matches the shell expression.
  22. */
  23. Object.defineProperty(exports, "__esModule", { value: true });
  24. function shExpMatch(str, shexp) {
  25. const re = toRegExp(shexp);
  26. return re.test(str);
  27. }
  28. exports.default = shExpMatch;
  29. /**
  30. * Converts a "shell expression" to a JavaScript RegExp.
  31. *
  32. * @api private
  33. */
  34. function toRegExp(str) {
  35. str = String(str)
  36. .replace(/\./g, '\\.')
  37. .replace(/\?/g, '.')
  38. .replace(/\*/g, '.*');
  39. return new RegExp(`^${str}$`);
  40. }
  41. //# sourceMappingURL=shExpMatch.js.map