
Security News
Attackers Are Hunting High-Impact Node.js Maintainers in a Coordinated Social Engineering Campaign
Multiple high-impact npm maintainers confirm they have been targeted in the same social engineering campaign that compromised Axios.
str-async-replace
Advanced tools
The AsyncReplace class provides asynchronous string replacement methods. It allows you to perform string replacements with flexibility, including support for regular expressions and asynchronous replacement functions.
AsyncReplace(inputString: string)AsyncReplace.inputString (string): The input string to perform replacements on.TypeError if inputString is null, undefined, an empty string, or not a string.async replace(searchValue: string | RegExp, replaceValue: string | ((substring: string, ...args: any[]) => Promise<string> | string), replaceLimit: number = 1): Promise<AsyncReplace>searchValue in the input string with the specified replaceValue.searchValue (string | RegExp): The value to search for in the input string. Can be a string or regular expression.replaceValue (string | ((substring: string, ...args: any[]) => Promise<string> | string)): The value to replace the search value with. Can be a string, function, object with a toString method, or an async function.replaceLimit (number, optional): The maximum number of replacements to make. Must be a positive integer greater than zero. Defaults to 1.Promise<AsyncReplace>: A new AsyncReplace instance with the replacements made.TypeError if:
searchValue is null, undefined, not a string, or not a regular expression.replaceLimit is not a positive integer greater than zero.replaceValue is null, an empty string, not a string, not a function, not an object with a toString method, or not an async function.const inputText = "Hello, World!";
const asyncReplacer = new AsyncReplace(inputText);
(async () => {
try {
const newString = await asyncReplacer.replace("World", async () => {
// Simulate a delay, e.g., an asynchronous HTTP request.
await new Promise(resolve => setTimeout(resolve, 2000));
return "Response from the server";
});
console.log(newString.toString()); // Output after 2 seconds: "Hello, Response from the server!"
} catch (error) {
console.error(error);
}
})();
async replaceAll(searchValue: string | RegExp, replaceValue: string | ((substring: string, ...args: any[]) => Promise<string> | string)): Promise<AsyncReplace>searchValue in the input string with the replaceValue provided.searchValue (string | RegExp): The value to search for in the input string. Can be a string or regular expression.replaceValue (string | ((substring: string, ...args: any[]) => Promise<string> | string)): The value to replace the search value with. Can be a string, function, object with a toString method, or an async function.Promise<AsyncReplace>: A new AsyncReplace instance with the replacements made.TypeError if:
searchValue is null, undefined, not a string, or not a regular expression.replaceValue is null, an empty string, not a string, not a function, not an object with a toString method, or not an async function.const inputText = "Hello, World!";
const asyncReplacer = new AsyncReplace(inputText);
(async () => {
try {
const newString = await asyncReplacer.replaceAll("World", async () => {
// Simulate a delay, e.g., an asynchronous HTTP request.
await new Promise(resolve => setTimeout(resolve, 2000));
return "Response from the server";
});
console.log(newString.toString()); // Output after 2 seconds: "Hello, Response from the server!"
} catch (error) {
console.error(error);
}
})();
async replaceMany(replacements: { search: string | RegExp, replace: string | ((substring: string, ...args: any[]) => Promise<string> | string) }[]): Promise<AsyncReplace>replacements (array of objects): An array of objects containing the search string or regular expression, and its corresponding replacement string or function to be executed.Promise<AsyncReplace>: A new AsyncReplace instance with the replacements made.TypeError if:
replacements parameter is not an array of objects.const inputText = "The quick brown fox jumps over the lazy dog.";
const asyncReplacer = new AsyncReplace(inputText);
(async () => {
try {
const replacements = [
{ search: "quick", replace: async () => {
// Simulate a delay, e.g., an asynchronous database query.
await new Promise(resolve => setTimeout(resolve, 2000));
return "fast";
}},
{ search: /brown/, replace: "red" },
{ search: "fox", replace: "rabbit" },
];
const newString = await asyncReplacer.replaceMany(replacements);
console.log(newString.toString()); // Output after 2 seconds: "The fast red rabbit jumps over the lazy dog."
} catch (error) {
console.error(error);
}
})();
async replaceAllMany(replacements: { search: string | RegExp, replace: string | ((substring: string, ...args: any[]) => Promise<string> | string) }[]): Promise<AsyncReplace>replaceAll method.replacements (array of objects): An array of objects containing the search string or regular expression, and its corresponding replacement string or function to be executed.Promise<AsyncReplace>: A new AsyncReplace instance with the replacements made.TypeError if:
replacements parameter is not an array of objects.const inputText = "The quick brown fox jumps over the lazy dog.";
const asyncReplacer = new AsyncReplace(inputText);
(async () => {
try {
const replacements = [
{ search: "quick", replace: async () => {
// Simulate a delay, e.g., an asynchronous database query.
await new Promise(resolve => setTimeout(resolve, 2000));
return "fast";
}},
{ search: /brown/, replace: "red" },
{ search: "fox", replace: "rabbit" },
];
const newString = await asyncReplacer.replaceAllMany(replacements);
console.log(newString.toString()); // Output after 2 seconds: "The fast red rabbit jumps over the lazy dog."
} catch (error) {
console.error(error);
}
})();
toString(): stringAsyncReplace.string: The input string.This documentation provides an overview of the AsyncReplace class and its methods, including their descriptions, parameters, return types, potential exceptions and examples that simulate delays for illustrative purposes. Use this documentation as a reference when working with the AsyncReplace class.
FAQs
An asynchronous string replacement library
We found that str-async-replace demonstrated a not healthy version release cadence and project activity because the last version was released a year ago. It has 1 open source maintainer collaborating on the project.
Did you know?

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Security News
Multiple high-impact npm maintainers confirm they have been targeted in the same social engineering campaign that compromised Axios.

Security News
Axios compromise traced to social engineering, showing how attacks on maintainers can bypass controls and expose the broader software supply chain.

Security News
Node.js has paused its bug bounty program after funding ended, removing payouts for vulnerability reports but keeping its security process unchanged.