October DealsAmazon USOctober deal check: compare before you payAmazon US: current deals, useful picks and tech finds.Check DealsClean PCRecommendedOne scan can reveal what keeps slowing WindowsLook for cleanup and repair opportunities.Run ScanOctober DealsAmazon USDeal season is back - check today's better picksAmazon US: current deals, useful picks and tech finds.See Picks×
Skip to content
World desk1 min

How to Remove a Substring from a String in TypeScript

Use replace() to remove the first literal match or replaceAll() to remove every match. Both return a new string; regex arguments have different rules.
Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

Use text.replace(target, "") to remove the first literal occurrence of a substring, or text.replaceAll(target, "") to remove every literal occurrence. Both methods return a new string, so assign or otherwise use the result.

Remove the first occurrence or every occurrence

When the search pattern is a string, replace() removes only its first match. replaceAll() removes every match of that string.

const text = "one fish, two fish";
const target = "fish";

const firstOnly = text.replace(target, "");
// "one , two fish"

const everywhere = text.replaceAll(target, "");
// "one , two "

These methods do not modify the original string. The examples store each returned value in a new variable; if you need the modified text later, use that returned value.

Remove matches using a pattern

Use a regular expression when the match should follow a pattern rather than equal a literal substring. Add the g flag to remove all matches with replace():

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.
const cleaned = text.replace(/pattern/g, "");

If you pass a regular expression to replaceAll(), it must have the g flag; otherwise, JavaScript throws a TypeError. For removing every occurrence of a literal target, pass the target as a string to replaceAll(). Building a regular expression dynamically from user-provided text can cause punctuation in that text to be interpreted as regex syntax.

Check TypeScript and runtime support

These are JavaScript string methods used in TypeScript. The TypeScript TSConfig reference lists string.replaceAll in the ES2021 library declarations. Confirm that your project includes the appropriate library declarations and that the JavaScript runtime where the code will run implements the method: TypeScript declarations provide type information, not a runtime implementation.

Rank #2
TypeScript Programming Language - Software Engineer & Coder T-Shirt
  • TypeScript implements a superset of syntax for strictly typed development, facilitating deep static analysis and enhanced development environment integration. The compiler translates source into standard script formats, ensuring parity across any runtime.
  • TypeScript is ideal for front-end developers, full-stack engineers, and software architects who build large-scale web applications. It serves those looking to improve code excellence, reduce bugs through static checking, and maintain complex projects more.
  • Lightweight, Classic fit, Double-needle sleeve and bottom hem

For ordinary string values in type annotations, use the primitive string type rather than the boxed String type.

Independent reader supportYour contribution helps us test, update, and keep practical guides available for everyone.Support on Ko-Fi

References

Product prices and availability are accurate as of the date/time indicated and are subject to change. Any price and availability information displayed on Amazon at the time of purchase will apply.

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

Leave a Reply

Your email address will not be published. Required fields are marked *

What’s actually slowing this PC down?

Pick the symptom - the matching free tool is one click away.

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

More from the Wire

  1. World desk4 min
    How to Spot an AI Voice Scam Before Sending MoneyDon’t rely on how a caller sounds. Pause, call back through a known number, and verify the emergency with another trusted person before sending money.
  2. Mountain View desk4 min
    Google’s SynthID Detector: How to Check AI-Generated Images, Video and AudioGoogle’s SynthID Detector looks for an embedded watermark in supported images, video and audio. Here is what its results do—and do not—show.
  3. Redmond desk20 min
    How to create a link to File or Folder in Windows 11Windows 11 gives you several ways to point to a file or folder without moving or duplicating it. You can create a desktop shortcut,…
Recommended PC Tool
Recommended PC Tool
Crashes, No Sound, or Screen Glitches?Free driver scan
PC Slower Than It Used to Be?Free scan - under a minute

Two free Windows tools

One Free Minute Could Fix That PC

Before you go - each of these free tools takes about a minute and tackles what quietly slows a Windows PC down.

Special offer. View Outbyte info, uninstall instructions, EULA, and Privacy Policy.