Let’s see this in a full example.
var someString = "This is a short sentence." someString = someString.replace("short", ""); console.log(someString); #Output: This is a sentence.
Let’s take a look at another example.
One such manipulation is to remove words from a string variable.
The replace() function takes two arguments: the substring we want to replace, and the replacement substring. In this case, to remove a word, we pass the word (“word”) as the first argument, and an empty string (“”) as the second argument.
Let’s say we wanted to remove all instances of a word instead of just the first occurrence of that word. We would have to change our code above slightly. We will use some example text from our About page.
var someString = "This string with have the word string in it three times to show the effect of removing the word string." someString = someString.replace(/string/g, ""); console.log(someString); #Output: This with have the word in it three times to show the effect of removing the word .
Leave a Reply