String.prototype.replaceAll
ponyfill.
The
replaceAll()
method returns a new string with all matches of a pattern replaced by a replacement. The pattern can be a string or aRegExp
, and the replacement can be a string or a function to be called for each match.
npm install string-replace-all-ponyfill --save
import replaceAll from 'string-replace-all-ponyfill';
replaceAll('abba', 'b', 'c'); // acca
You can use named export preferNative
if you wish to use native
implementation if it’s available. In all other cases, ponyfill will be used.
Beware of
caveats!
Returns: string
Type: string
String to replace.
Type: string|RegExp
If string
, stringToReplace
will be searched for this value.
If RegExp
, the matches are replaced with replaceValue
or the value returned
by the specified function. A RegExp
without the global flag will throw a
TypeError
.
Type: string|Function
If string
, searchValue
will be replaced with this value. A number of
special replacement patterns
are supported.
If Function
, it’s invoked to create the new substring which is used to replace
the matches of searchValue
parameter. See
arguments for the function).
Tested in IE11+ and all modern browsers.
Test suite is taken and modified from following packages:
For automated tests, run npm run test:automated
(append :watch
for watcher
support).
MIT © Ivan Nikolić