
Security News
Axios Maintainer Confirms Social Engineering Attack Behind npm Compromise
Axios compromise traced to social engineering, showing how attacks on maintainers can bypass controls and expose the broader software supply chain.
string-replace-utils
Advanced tools
A small library which exposes some helpful string-replacement functions, including exact types
A small library which exposes some helpful string-replacement functions, including exact types. Types used here come from rolling-ts-utils.
⚠️ Pay attention to the use of
as constthroughout this document. Not usingas constafter string and array initialisation will result in generic types being used, or unexpected behaviour!
npm install --save string-replace-utils
import { replace } from "string-replace-utils";
const str = "This is an example {noun}" as const;
const sentence = replace(str, "{noun}", "dog");
// ^? typeof sentence = "This is an example dog"
replace(str, key, value)str: The original stringkey: The substring to be replacedvalue: The string to be inserted in place of the first occurance of keyconst str = "This is an example {noun}" as const;
const sentence = replace(str, "{noun}", "dog");
// ^? typeof sentence = "This is an example dog"
replaceGlobal(str, key, value)str: The original stringkey: The substring to be replacedvalue: The string to be inserted in place of ALL occurances of keyconst str = "This is {word} {word} {thing}" as const;
const sentence = replaceGlobal(str, "{word}", "dog");
// ^? typeof sentence = "This is dog dog {thing}"
replaceOrdered(str, values)str: The original stringvalues: An array containing the strings to be inserted in place of, in order, each occurance of {string}, where string is any string{string} is found, the original string is returnedconst str = "This is {article} {adjective} {noun}." as const;
const sentence = replaceOrdered(str, ["a", "sneaky", "cat"] as const);
// ^? typeof sentence = "This is a sneaky cat."
replaceMultiple(str, keys, values)str: The original stringkeys: An array containing the substrings to be replacedvalues: An array of strings to be inserted in place of the key at the same position in the Keys arrayconst str = "This is {article} {adjective} {noun}." as const;
const sentence = replaceMultiple(
str,
["{article}", "{adjective}", "{noun}"] as const,
["a", "sneaky", "cat"] as const
);
// ^? typeof sentence = "This is a sneaky cat."
replaceAll(str, value)str: The original stringvalue: The string to be inserted in place of ALL occurences of {string}const str = "This is {article} {adjective} {noun}." as const;
const sentence = replaceAll(str, "cat");
// ^? typeof sentence = "This is a cat cat cat."
A weak version of replaceMultiple is provided in weak.ts. This function does not support exact types, but allows key-value pairs to be passed as an array of {key, value} objects.
This was more useful in the past when the replaceMultiple function had the same functionality as the current replaceOrdered function (thus this was the only way to replace specified substrings):
const str = "This is {article} {adjective} {noun}." as const;
const sentence = weakReplaceMultiple(str, [
{ key: "{article}", value: "a" },
{ key: "{adjective}", value: "sneaky" },
{ key: "{noun}", value: "cat" },
]);
// ^? "This is a sneaky cat."
// typeof sentence = string
String Replace Utils also provides a class, Replaceable, containing all of the methods provided by the pure functions, to allow for chaining methods.
As the class extends String, it can be used in the same way as a normal string (though asserting as string, or widening accepted parameter-types to include Replaceable<string> may be necessary to prevent Type errors on functions which take string parameters). Alternatively, use .valueOf() to get the raw string, as in the examples below.
⚠️ Some methods do not share the same name as the pure functions:
replace becomes extReplace to avoid conflict with String.prototype.replace()replaceAll becomes extReplaceAll to avoid conflict with String.prototype.replaceAll()const str = new Replaceable("This is {article} {adjective} {noun}." as const);
const sentence = str
.extReplace("{article}", "a")
.extReplace("{adjective}", "sneaky")
.extReplace("{noun}", "cat")
.valueOf();
// ^? typeof sentence = "This is a sneaky cat."
const str = new Replaceable("This is {article} {adjective} {noun}." as const);
const sentence = str
.replaceMultiple(
["{article}", "{adjective}", "{noun}"] as const,
["a", "pesky", "goose"] as const
)
.valueOf();
// ^? typeof sentence = "This is a pesky goose."
FAQs
A small library which exposes some helpful string-replacement functions, including exact types
We found that string-replace-utils 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
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.

Security News
The Axios compromise shows how time-dependent dependency resolution makes exposure harder to detect and contain.