How do I match specified words in a sentence? If I have the sentence "lord of the rings" and want to remove "of" and "the" how do I write the regex?
I don't want it to match anything in for example "don't match them" or "don't match often". It should match when "the" or "of" is in the beginning or end of the row as well.
The problem I encountered is when the words follow each other like in the example with "lord of the rings". When they share a space so to say.
The best I could manage is something like this:
/^(the|of) | (the|of)$| (of|the) /gmi
But it doesn't solve all my problems.
This is a perfect task for word boundaries
\b(of|the)\b
You can use the lookahead group (?= ) [doc]:
/^(the|of) | (the|of)$| (of|the)(?= )/gmi
Demo : https://regex101.com/r/Bo4Ur1/1
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With