replace.js 1.1 KB

123456789101112131415161718192021222324252627282930313233
  1. import _curry3 from "./internal/_curry3.js";
  2. /**
  3. * Replace a substring or regex match in a string with a replacement.
  4. *
  5. * The first two parameters correspond to the parameters of the
  6. * `String.prototype.replace()` function, so the second parameter can also be a
  7. * function.
  8. *
  9. * @func
  10. * @memberOf R
  11. * @since v0.7.0
  12. * @category String
  13. * @sig RegExp|String -> String -> String -> String
  14. * @param {RegExp|String} pattern A regular expression or a substring to match.
  15. * @param {String} replacement The string to replace the matches with.
  16. * @param {String} str The String to do the search and replacement in.
  17. * @return {String} The result.
  18. * @example
  19. *
  20. * R.replace('foo', 'bar', 'foo foo foo'); //=> 'bar foo foo'
  21. * R.replace(/foo/, 'bar', 'foo foo foo'); //=> 'bar foo foo'
  22. *
  23. * // Use the "g" (global) flag to replace all occurrences:
  24. * R.replace(/foo/g, 'bar', 'foo foo foo'); //=> 'bar bar bar'
  25. */
  26. var replace =
  27. /*#__PURE__*/
  28. _curry3(function replace(regex, replacement, str) {
  29. return str.replace(regex, replacement);
  30. });
  31. export default replace;