I have the need to write a RegEx to use in my javascript so I can match a set of three consecutive word. This three word will be variable know as "before", "error", "after". The thing is "error" is always there but as it can be the anchor at start or end of the sentence, "before" or "after" can be missing. So to illustrate :
If before= "this" after = "that" error="fail"
In the sentence : test = "this fail that, but fail is not part of the result but can be in the case it is like this, fail"
The result will be :
this fail that
this fail
only 2 of them are correctly return as they have the "error" word and at least one of the two side word. They can be symboles between the word as I don't get the punctuation.
I'm trying to learn RegEx but so far I only manage to retreive the error word with something like : new RegExp("\\b" + motErreur + "\\b", "gi");
And the try I did for the three word do not seems to work correctly :
pattern = @"(?:^\W*|(?<"+before+">\w+)\W+)" + error + @"(?:\W+(?<"+after+">\w+)|\W*$)";
As pattern if taken from an exemple in C# in my code and need it in Javascript I don't know if it is what make him fail.
How can I do this with a simple RegEx ? the purpose is then to replace the part of the sentence return (I already got the function written for that, I only fail with this RegEx).
(?:^\W*|(\w+)\W+)fail(?:\W+(\w+)|\W*$), right?