fast-replaceall
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 toString#replace()
, but you would then have to first escape the string anyways.
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]
Default0
. Index at which to start replacing. -
caseInsensitive
[boolean]
Defaultfalse
. Whether or not substring matching should be case-insensitive.
-