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():
Recommended Free Tools
#1 Best Overall
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 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.
Quick Recap
Best Value
References
- MDN: String.prototype.replace()
- MDN: String.prototype.replaceAll()
- TypeScript TSConfig: lib
- TypeScript: a brief introduction
- TypeScript: everyday types
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.
Do these 3 things before closing this tab:
1Clear out junk files and repair common Windows errors2Fix the driver behind crashes, sound loss and screen glitches3Repair Windows errors before they cause bigger problems




