fast-replaceall

1.0.2 • Public • Published

fast-replaceall

npm package

Similar to String#replace(), but supports replacing multiple matches. You could achieve something similar by putting the string in a RegExp constructor with the global flag and passing it to String#replace(), but you would then have to first escape the string anyways.

NPM version NPM Downloads

Installation

npm install fast-replaceall --save

Usage

const replaceAll = require('fast-replaceall');
 
const string = 'The quick brown fox jumps over the lazy dog. If the dog reacted, was it really lazy?';
 
replaceAll(string, 'dog', 'monkey');
//=> 'The quick brown fox jumps over the lazy monkey. If the monkey reacted, was it really lazy?'

API

export default function (
	str: string,
	substr: string,
	replacement: (match: string, offset: number, str: string) => string | any,
	options?: object
): string

Returns a new string with all substr matches replaced with replacement.

  • str <string> String to work on.
  • substr <string> String to match in input.
  • replacement <string | function> Replacement for substr matches.
  • options [object]
    • fromIndex [number] Default 0. Index at which to start replacing.
    • caseInsensitive [boolean] Default false. Whether or not substring matching should be case-insensitive.

Package Sidebar

Install

npm i fast-replaceall

Weekly Downloads

81

Version

1.0.2

License

MIT

Unpacked Size

14.4 kB

Total Files

9

Last publish

Collaborators

  • fengxinming