- Source:
Methods
replace(subject, search, replace) → {String}
Offers similar functionality to PHP's str_replace or ES2021's replaceAll and avoids RegExps for this task. Replaces occurrences of search in subject with replace. search and replace may be arrays. If search is an array and replace is a string, all phrases in the array will be replaced with one string. If replace is an array itself, phrases and replacements are matched by index. Missing replacements are treated as an empty string (for example: if your array lengths do not match).
Uses String.prototype.replaceAll internally, if available.
Parameters:
Name | Type | Description |
---|---|---|
subject |
String | the string to replace in |
search |
String | Array.<String> | the string(s) to replace |
replace |
String | Array.<String> | the string(s) to replace the search string(s) |
- Source:
Returns:
the modified string
- Type
- String
Example
const sanitizedString = replace([':', '#', '-'], '_', exampleString);