简体   繁体   中英

How to remove an array of words from a string in javascript?

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 ?

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

wordArray.forEach(word => {
    sentence = sentence.replace(word, '');
})

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.

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)

You can try this:

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

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM