I have a solve a complex problem: I want to filter out substrings from a possible long text. There are certain keywords that indicate a substring. Only if a keyword is preceeded by at least one character, which is not a white space or a different keyword, should match. Then also every character they keyword is preceeded by should be included in the match. I want to use a regex expression in JavaScript for this.
My keywords are: ":yellow:", ":black:", ":green:", ":blue:" ":red:"
For example I have a Text like this: " :green: aba :red: gd efg:blue: :yellow: sdg:red: sea gea e :black: "
Now I want to use match() on this string with a re that gives me these matches: " aba :red:", "gd efg:blue":, "sdg:red:", sea gea e :black:
:green: at the start should not be matched, because it is not preceded by a character. :yellow: should also not be matched, because it is preceded by a different keyword (in this case :blue:)
I have tried to use negative lookahead expressions (like (?!)) to prevent matching when keywords preceed other keywords. But it didn't quite give me the results I am looking for.
/((?!(:yellow:|:black:|:green:|:blue:|:red:))\S+\s*)+(:yellow:|:black:|:green:|:blue:|:red:)/g
let ar1 = text1.match(re1);
console.log(ar1);
this is my output: [ 'green: aba :red:', 'gd efg:blue: :yellow:', 'sdg:red: sea gea e :black:' ]
but i want this:
[ ' aba :red:', 'gd efg:blue: ', 'sdg:red:', 'sea gea e :black:' ]