I have following string.
var str = "abc, abcd, abc, abcf, abc, abc";
I want to remove abc from a given string with , (space). Basically, I want output something like below.
var output = "abcd, abcf";
Note:- If possible give regex solution(with one replace only).
Things I have tried:-
1) 1st approach
var str = "abc, abcd, abc, abcf, abc, abc";
var w = "abc";
var output = str.split(", ").filter(x => !(x === w)).join(", ");
console.log(output);
Here I have used split, then filter and then join, basically 3 array operations. (which I think not good when performance is concerned).
2) 2nd approach
var str = "abc, abcd, abc, abcf, abc, abc";
var w = "abc";
var regex = new RegExp("\\b" + w + "\\b", "ig");
var output = str.replace(regex,"");
output = output.replace(/, , /ig,", ");
output = output.replace(/^, |, $/ig,"");
console.log(output);
Here I am using three times replace.
Is it possible achieve in simpler way?