How to remove text between parentheses in a string using TypeScript

1 Answer

0 votes
function cleanString(text: string): string {
  // Step 1: Remove parentheses and their content (non-greedy)
  let cleaned: string = text.replace(/\([^)]*\)/g, "");

  // Step 2: Collapse multiple spaces into one
  cleaned = cleaned.replace(/\s+/g, " ").trim();

  return cleaned;
}

const original = "Hello (remove this) from the future (and this too)";
const result: string = cleanString(original);

console.log("Original:", original);
console.log("Cleaned :", result);


/*
run:

"Original:",  "Hello (remove this) from the future (and this too)" 
"Cleaned :",  "Hello from the future" 

*/

 



answered Dec 18, 2025 by avibootz

Related questions

...