/**
* Remove all parentheses and the text inside them.
*
* @param text The input string
* @return The cleaned string
*/
function removeParenthesesWithContent(text: string): string {
// Remove parentheses and everything inside them
let cleaned: string = text.replace(/\([^)]*\)/g, " ");
// Collapse multiple spaces into one
cleaned = cleaned.replace(/\s+/g, " ");
// Final trim of leading/trailing spaces
return cleaned.trim();
}
const str: string =
"(An) API (API) (is a) (connection) connects (between) computer programs";
const output: string = removeParenthesesWithContent(str);
console.log(output);
/*
run:
API connects computer programs
*/