I am attempting to build a find and remove type functionality with regex. I was able to get to the point where I could remove strings, but discovered that strings with characters could not be removed. When, using a normal string such as var word = "is"
, it seems to work fine until it encounters a .
, and then I get strange unwanted output.
Some other unwanted occurrences also developed when incorporating characters into the strings I wanted to remove, for example (note that var word = "is."
and not is
in the code below:
var myarray = ["Dr. this is", "this is. iss", "Is this IS"]
var my2array = []
var word = "is."
//var regex = new RegExp(`\\b${word}\\b`, 'gi');
var regex = new RegExp('\\b' + word + '\\b', 'gi');
for (const i of myarray) {
var x = i.replace(regex, "")
my2array.push(x)
}
myarray = my2array
console.log(myarray)
["Dr. this is", "this is. ", "this IS"]
This ^ is wrong in several ways (for some reason iss
is gone, is.
remains - which was the main string I was trying to remove, the first is
in the last index is gone...)
I.E. my desired output in this case would be ["Dr. this is", "this iss", "Is this IS"]
I also tried using template literal, as can be seen in my commented out code.
The goal is to simply remove whatever might be the value in var word
from my array. Whether the value be a regular string, a string with characters, or just characters. (And of course within the framework of the breaks I have).