
Product
Socket for Jira Is Now Available
Socket for Jira lets teams turn alerts into Jira tickets with manual creation, automated ticketing rules, and two-way sync.
string-replace-async
Advanced tools
A vesion of "string".replace() that knows how to wait
$ npm install string-replace-async
Note: If you're using pre-14 version of Node or your node codebase isn't converted to ES Modules yet, please use Version 2 specifically!
$ npm install string-replace-async@^2.0.0
import replaceAsync from "string-replace-async";
await replaceAsync("#rebeccapurple", /#(\w+)/g, async (match, name) => {
let color = await getColorByName(name);
return "#" + color + " (" + name + ")";
});
Say you have a task of replacing color names with their respective hex codes.
let spec = "I want background to be #papayawhip and borders #rebeccapurple.";
// make it "I want background to be #FFEFD5 (papayawhip) and borders #663399 (rebeccapurple).";
Luckily, strings in JavaScript have this handy replace method built in, so you use it.
spec.replace(/#(\w+)/g, (match, name) => {
let color = getColorByName(name);
return "#" + color + " (" + name + ")";
});
Time passes, a new requirement emerges: now you have to query a database for custom colors. This is an async operation, so naturally you convert getColorByName into async function.
Turns out it has a cost: now all the code above should also be async. You try this:
await spec.replace(/#(\w+)/g, async (match, name) => {
let color = await getColorByName(name);
return "#" + color + " (" + name + ")";
});
Unfortunately, this code doesn't work as you expect. Built in menthod wasn't designed to work as async function.
This is where string-replace-async comes in:
await replaceAsync(spec, /#(\w+)/g, async (match, name) => {
let color = await getColorByName(name);
return "#" + color + " (" + name + ")";
});
Yay!
string-replace-async is nothing but direct String.prototype.replace replacement that awaits your function and returns a Promise for results.
API is String.prototype.replace(), except the first argument is a string itself.
Runs replace and waits for it to resolve before replacing searchValue with results. If searchValue is a global RegExp, replace will be called concurrently for every match.
Type: string
Required
An input string.
Type: regexp, string
An expression to match substrings to replace.
Type: function, string
A function that takes several arguments and returns a promise. Resolved value will be used as replacement substring.
Previously this module had an additional function seq() that ran replace functions one by one instead of all at once. We decided to remove it to narrow our scope. Here's a snippet that achieves the same effect:
let sequence = Promise.resolve();
let seq = (fn) => (...args) => (sequence = sequence.then(() => fn(...args)));
await replaceAsync(
"#rebeccapurple, #papayawhip",
/#(\w+)/g,
seq(async (match, name) => {
let color = await getColorByName(name);
return "#" + color + " (" + name + ")";
})
);
MIT © Dmitrii Sobolev
FAQs
Asynchronous String.prototype.replace()
The npm package string-replace-async receives a total of 94,741 weekly downloads. As such, string-replace-async popularity was classified as popular.
We found that string-replace-async 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.

Product
Socket for Jira lets teams turn alerts into Jira tickets with manual creation, automated ticketing rules, and two-way sync.

Company News
Socket won two 2026 Reppy Awards from RepVue, ranking in the top 5% of all sales orgs. AE Alexandra Lister shares what it's like to grow a sales career here.

Security News
NIST will stop enriching most CVEs under a new risk-based model, narrowing the NVD's scope as vulnerability submissions continue to surge.