How to remove parentheses and the text inside them from a string in TypeScript

1 Answer

0 votes
/**
 * 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

*/

 



answered 2 days ago by avibootz

Related questions

...