Here Is My Regex Code:
/fun(niest|!ny$)?/ig
How would I get the word "fun" or "funniest" but not the word "funny" through regex, here is what I have. Is there any way of doing this, if so please help!
You can use word boundaries \b and an optional group (?:niest)?:
/\bfun(?:niest)?\b/ig
See the regex demo
The pattern matches:
\b - leading word boundaryfun - literal character sequence fun(?:niest)? - an optional (one or zero occurrences) niest literal character sequence (not captured into any group since the group is non-capturing, i.e. used only for grouping)\b - trailing word boundary.Your fun(niest|!ny$)? matches fun, or funniest or fun!ny that is at the end of the string.
funny|honey will match either funny or honey. Could you be more precise?\b (as I showed) or non-ambiguous /(^|\W)(fun(?:niest)?)(?=\W|$)/ig (but then you will need to use backreferences wisely if replacing, or do more post-processing if matching)./\b(funniest|fun(?!ny\b)\w*)\b/ig - otherwise I see no use for the lookahead. The thing is, the words are to be matched as whole words in the current scenario, and the 2 words are to be matched only. If OP says any word having or starting with fun should be matched, but not funny, then the lookahead solution will be the best.
/fun(?!ny)(niest)?/