This module provides a class for replacing all occurrences of a substring in a string with a specified replacement string.
To use this module, you should have the "node-string-methods" npm package installed. You can install it using npm or yarn:
npm install node-string-methods
const Replace = require('node-string-methods/replace');
// Create an instance of the Replace class with a string, search string, and replacement string
const str = 'Hello, world! Hello, universe!';
const search = 'Hello';
const replacement = 'Hi';
const replacer = new Replace(str, search, replacement);
// Execute the replace operation
const result = replacer.execute();
console.log(result);
// Output: 'Hi, world! Hi, universe!'
str
(String): The input string in which you want to replace occurrences of a substring.search
(String): The substring to search for instr
.replacement
(String): The string to replace occurrences ofsearch
with.
Creates a new instance of the Replace
class with the provided input string, search string, and replacement string.
Replaces all occurrences of the search string in the input string with the specified replacement string.
- Returns: A new string with all occurrences replaced.
const Replace = require('node-string-methods/replace');
const str = 'Hello, world! Hello, universe!';
const search = 'Hello';
const replacement = 'Hi';
const replacer = new Replace(str, search, replacement);
const result = replacer.execute();
console.log(result);
// Output: 'Hi, world! Hi, universe!'