What is replaceall?
The replaceall npm package is a simple utility for replacing all instances of a substring within a string. It provides a straightforward API to perform global replacements without the need for regular expressions.
What are replaceall's main functionalities?
Basic Replacement
This feature allows you to replace all occurrences of a substring with another substring. In this example, all instances of 'foo' are replaced with 'bar'.
const replaceall = require('replaceall');
const result = replaceall('foo', 'bar', 'foo is foo');
console.log(result); // 'bar is bar'
Case Sensitive Replacement
This feature performs case-sensitive replacements. Only the exact matches of the substring will be replaced. In this example, only 'Foo' is replaced with 'bar', while 'foo' remains unchanged.
const replaceall = require('replaceall');
const result = replaceall('Foo', 'bar', 'Foo is foo');
console.log(result); // 'bar is foo'
Replacing Special Characters
This feature allows you to replace special characters within a string. In this example, all instances of '?' are replaced with '!'.
const replaceall = require('replaceall');
const result = replaceall('?', '!', 'Hello? How are you?');
console.log(result); // 'Hello! How are you!'
Other packages similar to replaceall
string-replace-all
The string-replace-all package provides similar functionality to replaceall, allowing you to replace all instances of a substring within a string. It also supports case-sensitive replacements and is a lightweight alternative.
replace-string
The replace-string package is another alternative that offers global string replacement without the need for regular expressions. It is simple to use and provides similar functionality to replaceall.
string.prototype.replaceall
The string.prototype.replaceall package is a polyfill for the String.prototype.replaceAll method introduced in ECMAScript 2021. It provides native-like functionality for replacing all instances of a substring within a string.
replaceall
![devDependency Status](https://david-dm.org/leecrossley/replaceall/dev-status.png)
Replace all instances in a JavaScript string.
Install with npm
npm install replaceall
To then include replaceall in your node app:
var replaceall = require("replaceall");
Using replaceall
var result = replaceall("instances of this", "with this string", "in this string");
Example
var original = "hello world goodbye world";
replaceall("world", "everyone", original);
replaceall("l", "z", original);
License
MIT License