string-replace-all-ponyfill

1.0.1 • Public • Published

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ć

/string-replace-all-ponyfill/

    Package Sidebar

    Install

    npm i string-replace-all-ponyfill

    Weekly Downloads

    269

    Version

    1.0.1

    License

    MIT

    Unpacked Size

    19.8 kB

    Total Files

    10

    Last publish

    Collaborators

    • niksy