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

6

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"))

Sign up to request clarification or add additional context in comments.

1 Comment

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
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 Comment

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.
2

You can try this:

function removeFromString(arr, str){
    arr.forEach(w => str = str.replace(w, ''));
    return str;
}

1 Comment

Hi Mo, please add a description to your answer as to how this solves the question.
0
wordArray.forEach(word => {
    sentence = sentence.replace(word, '');
})

Comments

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.

Comments

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"});

Comments

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.