Important: This documentation covers Yarn 1 (Classic).
For Yarn 2+ docs and migration guide, see yarnpkg.com.

Package detail

string-replace-all-ponyfill

niksy8.6kMIT1.0.1

String.prototype.replaceAll ponyfill.

string, replaceall, prototype, string.prototype.replaceall, ponyfill

readme

string-replace-all-ponyfill

Build Status BrowserStack Status

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 a RegExp, and the replacement can be a string or a function to be called for each match.

Install

npm install string-replace-all-ponyfill --save

Usage

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!

API

replaceAll(stringToReplace, searchValue, replaceValue)

Returns: string

stringToReplace

Type: string

String to replace.

searchValue

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.

replaceValue

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).

Browser support

Tested in IE11+ and all modern browsers.

Acknowledgments

Test

Test suite is taken and modified from following packages:

For automated tests, run npm run test:automated (append :watch for watcher support).

License

MIT © Ivan Nikolić

changelog

Changelog

Unreleased

1.0.1 - 2020-12-17

Added

  • Named export preferNative

Fixed

  • Handle non-primitive replacers
  • Use global instead of flags property on RegExp instance to handle IE11

1.0.0 - 2020-12-15

Added

  • Initial implementation