How to remove a given word from a string in JavaScript

1 Answer

0 votes
function removeWord(str, word) {
    if (str.includes(word)) {
        let tmp = word + " ";
        str = str.replaceAll(tmp, "");
        tmp = " " + word;
        str = str.replaceAll(tmp, "");
    }
    
    return str;
}
        
let str = "javascript c c++ c# python rust go";
const word = "rust";

str = removeWord(str, word);

console.log(str);




/*
run:

"javascript c c++ c# python go"

*/

 



answered Nov 19, 2022 by avibootz
...