input string
search expression
replacement value or function
new string containing replacement(s)
var str = 'beep';
var out = replace( str, 'e', 'o' );
// returns 'boop'
var str = 'Hello World';
var out = replace( str, /world/i, 'Mr. President' );
// returns 'Hello Mr. President'
var capitalize = require( `@stdlib/string/capitalize` );
var str = 'Oranges and lemons say the bells of St. Clement\'s';
function replacer( match, p1 ) {
return capitalize( p1 );
}
var out = replace( str, /([^\s]*)/gi, replacer);
// returns 'Oranges And Lemons Say The Bells Of St. Clement\'s'
Replace search occurrences with a replacement string.
Notes
string
as thesearch
value, the function replaces all occurrences. To remove only the first match, use a regular expression.