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

javascript remove specific character from string
javascript split string at index
string to array javascript
javascript remove part of string
javascript substring
javascript slice
javascript split string by comma
split string into array of words 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") )

JavaScript: Split a string and convert it into an array of words , Find the index of the array element you want to remove using indexOf, and then remove that index with splice. The splice() method changes the  There are three methods to remove the text from a string which are listed below: Method 1: Using replace() method: The replace method can be used to replace the specified string with another string. It takes two parameters, first is the string to be replaced and the second is the string which is replacing from the first string.

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)


How to remove text from a string in JavaScript?, How to Create a Dropdown List with Array Values using JavaScript ? Difference between lodash and Underscore · JavaScript | Super keyword · JavaScript |  Javascript sort array of objects in reverse chronological order. javascript,arrays,sorting. As PM 77-1 suggests, consider using the built–in Array.prototype.sort with Date objects. Presumably you want to sort them on one of start or end: jobs.sort(function(a, b) { return new Date(a.ys, - new Date(b.ys,; })

You can try this:

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

How to remove portion of a string after certain character in JavaScript, Return value: Returns a new array, having the splitted items. JavaScript String substring() Method: This method gets the characters from a string, between two  Using Splice to Remove Array Elements in JavaScript. The splice method can be used to add or remove elements from an array. The first argument specifies the location at which to begin adding or removing elements. The second argument specifies the number of elements to remove.

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

String.prototype.split(), See “How do you get a string to a character array in JavaScript?” on StackOverflow. Return value. An Array of strings, split at each point where the separator occurs in the given string. Removing spaces from a string. In the following  The split() method is used to split a string into an array of substrings, and returns the new array. Tip: If an empty string ("") is used as the separator, the string is split between each character. Note: The split() method does not change the original string.

You need something like this:

function newRemoveFromString(stringArray,string){

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


    return string;

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

JavaScript Array Methods, Converting Arrays to Strings When you work with arrays, it is easy to remove elements and add new Using delete may leave undefined holes in the array. The string in JavaScript can be converted into a character array by using the split() and Array.from() functions. Using String split() Function : The str.split() function is used to split the given string into array of strings by separating it into substrings using a specified separator provided in the argument.

JavaScript Array splice(): Delete, Insert, and Replace Elements in an , Deleting elements using JavaScript Array's splice() method. To delete For example: Assuming that you have an array named colors with three strings. Parameter Description; separator: Optional. Specifies the character, or the regular expression, to use for splitting the string. If omitted, the entire string will be returned (an array with only one item)

Converting JavaScript Arrays to Strings, How to convert JavaScript arrays to strings. str.slice(0, -sep.length); // remove last separator and return str } // pass array from example above to joinSubArrays​  Definition and Usage. The join() method returns the array as a string.. The elements will be separated by a specified separator. The default separator is comma (,). Note: this method will not change the original array.

Remove first character from a string in JavaScript, In this post, we will see how to remove first character from a string in JavaScript. Since strings are immutable in JavaScript, we can't in-place remove characters  The string itself is split by whitespaces within the string, creating an array of contiguous non-whitespace characters. There are then two loops for processing the words. The outer loop cycles through all of the words in the string, and the inner loop cycles through all of the stop words.

  • 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
  • this helped me. thanks.
  • @chongzixin you're welcome!
  • Hi Mo, please add a description to your answer as to how this solves the question.