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.

shExpMatch.js 997B

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