3

I have a function that can remove words from a string. It is :

  var removeFromString = function(oldStr, fullStr) { 
    return fullStr.split(oldStr).join(''); 
  };

I use it like this :

 console.log( removeFromString("Hello", "Hello World") ); // World
 console.log( removeFromString("Hello", "Hello-World") ); // -World

But the main problem :

 var str = "Remove one, two, not three and four"; 

Here we have to remove "one", "two" & "four". This can be done by :

var a = removeFromString("one, two,", str); // Remove not three and four
var b = removeFromString("and four", a); // Remove not three
console.log(b); // Remove not three

In the above example, I had to use the function twice. I want it to be something like this :

 var c = removeFromString(["one, two," , "and four"], str); // Remove not three
 console.log(c); // Remove not three

Yes, I actually want to upgrade the removeFromString function ! How can I do that ?

6 Answers 6

5

You can use join and build regex dynamically from the array and replace the matching values

function removeFromString(arr,str){
  let regex = new RegExp("\\b"+arr.join('|')+"\\b","gi")
  return str.replace(regex, '')
}

console.log(removeFromString(["one, two," , "and four"],"Remove one, two, not three and four" ));
console.log(removeFromString(["one" , "and four"],"Remove one, two, not three and four" ));
console.log(removeFromString(["Hello"], "Hello World") )


To cover cases where your word to match can have meta-characters you can expand the above example in this way

function escape(s) {
    return s.replace(/[-\/\\^$*+?.()|[\]{}]/g, '\\$&');
};

function removeFromString(arr, str) {
  let escapedArr = arr.map(v=> escape(v))
  let regex = new RegExp("(?:^|\\s)"+escapedArr.join('|') + "(?!\\S)", "gi")
  return str.replace(regex, '')
}

console.log(removeFromString(["one, two,", "and four"], "Remove one, two, not three and four"));
console.log(removeFromString(["one", "and four"], "Remove one, two, not three and four"));
console.log(removeFromString(["Hello"], "Hello World"))
console.log(removeFromString(["He*llo"], "He*llo World"))
console.log(removeFromString(["Hello*"], "Hello* World"))

1
  • I would just suggest sanitizing the strings to avoid building an invalid regexp, and making it able to search for special chars such as ., *, etc... More info: here Oct 14, 2019 at 12:42
4

Another approach using reduce:

function removeFromString(words, str) {
  return words.reduce((result, word) => result.replace(word, ''), str)
}

const str = "Remove one, two, not three and four"
const result  = removeFromString(["one, two, " , "and four"], str)

console.log(result)

1
  • Great use of Array.prototype.reduce. Just one remark: it would be better to use replaceAll instead of replace, if you want to replace all occurences of word.
    – cezar
    Oct 19, 2021 at 9:37
2

You can try this:

function removeFromString(arr, str){
    arr.forEach(w => str = str.replace(w, ''));
    return str;
}
1
  • Hi Mo, please add a description to your answer as to how this solves the question. Oct 14, 2019 at 21:18
0
wordArray.forEach(word => {
    sentence = sentence.replace(word, '');
})
0

You need something like this:

function newRemoveFromString(stringArray,string){

    for(var i=0;i<stringArray.length;i++){

        string=removeFromString(stringArray[i],string);

    }
    return string;
}

It loops through the words in the Array and removes them one by one.

0

try this

function replaceStr(myString,list)
  {
    $.each(list,function(i,item){
    myString=myString.replace('Hello', item);
    });   
  }
  
  /*Now use this fucntion like this */
replaceStr(yourstring,arrayOfWords);
or replaceStr(yourString,{"word1","word2"});

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.