match.js 972 B

123456789101112131415161718192021222324252627282930
  1. import _curry2 from "./internal/_curry2.js";
  2. /**
  3. * Tests a regular expression against a String. Note that this function will
  4. * return an empty array when there are no matches. This differs from
  5. * [`String.prototype.match`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String/match)
  6. * which returns `null` when there are no matches.
  7. *
  8. * @func
  9. * @memberOf R
  10. * @since v0.1.0
  11. * @category String
  12. * @sig RegExp -> String -> [String | Undefined]
  13. * @param {RegExp} rx A regular expression.
  14. * @param {String} str The string to match against
  15. * @return {Array} The list of matches or empty array.
  16. * @see R.test
  17. * @example
  18. *
  19. * R.match(/([a-z]a)/g, 'bananas'); //=> ['ba', 'na', 'na']
  20. * R.match(/a/, 'b'); //=> []
  21. * R.match(/a/, null); //=> TypeError: null does not have a method named "match"
  22. */
  23. var match =
  24. /*#__PURE__*/
  25. _curry2(function match(rx, str) {
  26. return str.match(rx) || [];
  27. });
  28. export default match;