I'm trying to remove all lingering spaces between tags. So I try to select them with a regex.
<span> </span>
^^^^^^^^
My regex is (?:>) +(?:<)
. I'm trying to exclude the >
and <
from the selection with a non-capturing group, but it doesn't seem to be working.
At the moment, these two regexes seem to do the exact same thing:
With non-capturing groups: (?:>) +(?:<)
Without non-capturing groups: > +<
I think my understanding of regex is not good enough, but I'm not sure. What's wrong here?
A non-capturing group doesn't capture the subpattern in a group (that you can refer later), however, all that have been matched in a non-capturing group is not excluded from the whole match result.
The way to solve the problem is to use lookarounds that are zero-width assertions. Lookarounds are only tests and are not part of the final result.
for spaces:
(?<=>) +(?=<)
for all whitespace-characters:
(?<=>)\s+(?=<)
(An other solution consists to use > +<
with ><
as replacement string)
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