-
Notifications
You must be signed in to change notification settings - Fork 136
Expand file tree
/
Copy pathreplace.js
More file actions
30 lines (29 loc) · 1 KB
/
replace.js
File metadata and controls
30 lines (29 loc) · 1 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
import coerceToString from 'helper/string/coerce_to_string';
/**
* Replaces the matches of `search` with `replace`. <br/>
*
* @function replace
* @static
* @since 1.0.0
* @memberOf Manipulate
* @param {string} [subject=''] The string to verify.
* @param {string|RegExp} search The search pattern to replace. If `search` is a string,
* a simple string match is evaluated and only the first occurrence replaced.
* @param {string|Function} replace The string or function which invocation result replaces `search` match.
* @return {string} Returns the replacement result.
* @example
* v.replace('swan', 'wa', 'u');
* // => 'sun'
*
* v.replace('domestic duck', /domestic\s/, '');
* // => 'duck'
*
* v.replace('nice duck', /(nice)(duck)/, function(match, nice, duck) {
* return 'the ' + duck + ' is ' + nice;
* });
* // => 'the duck is nice'
*/
export default function replace(subject, search, replace) {
const subjectString = coerceToString(subject);
return subjectString.replace(search, replace);
}