I prrobably should have mentioned that I want to use strings longer than a single character. like this;
let myArray = [
"Item 1 remove this",
"Item 2 remove that",
"Item 3 remove this also",
"Item and this one as well 4 ",
];
let replaceStrings = [
" remove this",
" remove that",
" remove this also",
" and this one as well ",
];
And return
[
"Item 1",
"Item 2",
"Item 3",
"Item 4 ",
];
Also I see that your code uses “src”, and I get an error "src is not defined’. What would I use in its place if I am wanting to use the script in a non html/ web environment.
I’ll have to do some homework on the ‘src’ property.
Put the ones that are longer first: the issue you have is that you have " remove this"then" remove this also". Regex pattern matches, left to right. And because " remove this" comes first, that matches, and the replace occurs. If you have [say] two potential matches and they are very similar, the difference being that one has some extra characters, put the longer one first.
tl/dr always match from most specific to least specific