I have developed a unique function that efficiently deletes specified words from a given string. Here is the function:
var removeFromString = function(wordList, fullStr) {
if (Array.isArray(wordList)) {
wordList.forEach(word => {
fullStr = fullStr.split(word).join('');
});
} else {
fullStr = fullStr.split(wordList).join('');
}
return fullStr;
};
To use this function, you can follow these examples:
console.log( removeFromString("Hello", "Hello World") ); // World
console.log( removeFromString("Hello", "Hello-World") ); // -World
However, a common issue arises with scenarios like:
var str = "Remove one, two, not three and four";
In this case, we need to delete "one", "two", and "four". To achieve this, you can do the following:
var c = removeFromString(["one, two," , "and four"], str); // Remove not three
console.log(c); // Remove not three
As you can see, this updated function enables you to remove multiple words at once by passing an array of words. This enhances the functionality of the removeFromString function. Feel free to give it a try!