What is replace-string?
The replace-string npm package is a simple utility for replacing all occurrences of a substring within a string with a new substring. It is useful for string manipulation tasks where you need to perform replacements in a straightforward and efficient manner.
What are replace-string's main functionalities?
Basic Replacement
This feature allows you to replace all occurrences of a specified substring within a string with a new substring. In this example, 'World' is replaced with 'Universe'.
const replaceString = require('replace-string');
const result = replaceString('Hello World', 'World', 'Universe');
console.log(result); // 'Hello Universe'
Case Sensitive Replacement
The replacement is case-sensitive by default. In this example, 'world' is not replaced because it does not match the case of 'World'.
const replaceString = require('replace-string');
const result = replaceString('Hello World', 'world', 'Universe');
console.log(result); // 'Hello World'
Replacing Special Characters
This feature allows you to replace substrings that include special characters. In this example, '$World$' is replaced with 'Universe'.
const replaceString = require('replace-string');
const result = replaceString('Hello $World$', '$World$', 'Universe');
console.log(result); // 'Hello Universe'
Other packages similar to replace-string
string-replace-all
The string-replace-all package provides similar functionality to replace-string, allowing you to replace all occurrences of a substring within a string. It offers a straightforward API and is useful for basic string replacement tasks.
replace
The replace package is a more versatile tool that can be used for replacing strings in files as well as in strings. It supports regular expressions and offers more advanced replacement options compared to replace-string.
string.prototype.replaceall
The string.prototype.replaceall package is a polyfill for the String.prototype.replaceAll method, which is a standard method in modern JavaScript. It provides a native way to replace all occurrences of a substring within a string, making it a more standardized option compared to replace-string.
replace-string
Replace all substring matches in a string
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.
With Node.js 16, this package is partly moot as there is now a String#replaceAll
method. However, it does not have a caseInsensitive
option.
Install
$ npm install replace-string
Usage
import replaceString from 'replace-string';
const string = 'My friend has a 🐑. I want a 🐑 too!';
replaceString(string, '🐑', '🦄');
API
replaceString(string, needle, replacement, options?)
Returns a new string with all needle
matches replaced with replacement
.
string
Type: string
The string to work on.
needle
Type: string
The string to match in input
.
replacement
Type: string | Function
The replacement for needle
matches.
If a function, it receives the matched substring, the match count, the original input, and the index in which the match happened (as measured from the original input):
import replaceString from 'replace-string';
replaceString('Foo 🐑 Bar', '🐑', (matchedSubstring, matchCount, input, matchIndex) => `${matchedSubstring}❤️`);
options
Type: object
fromIndex
Type: number
Default: 0
Index at which to start replacing.
caseInsensitive
Type: boolean
Default: false
Whether or not substring matching should be case-insensitive.
Related
- execall - Find multiple
RegExp
matches in a string