Let’s start with the following input.
Input = 'blue, blueblue, b l u e'
I want to match everything that is not the string ‘blue’. Note that blueblue should not match but single characters should (even if present in match string).
From this If I replace the matches with an empty string it should return:
Result = 'blueblueblue'
I have tried with [^\bblue\b]+
but this matches the last four single characters ‘b’, ‘l’,’u’,’e’
>Solution :
If you regex engine support the \K flag, then we can try:
/blue\K|.*?(?=blue|$)/gm
Demo
This pattern says to match:
bluematch "blue"\Kbut then forget that match|OR.*?match anything else until reaching(?=blue|$)the next "blue" or the end of the string
Edit:
On JavaScript, we can try the following replacement:
var input = "blue, blueblue, b l u e";
var output = input.replace(/blue|.*?(?=blue|$)/g, (x) => x != "blue" ? "" : "blue");
console.log(output);