trying to use regex to replace any white space with “ ”, inside of example html
<span someattr="a">and some words with spaces</span>
It’s a desktop app and this html is coming to/from a third party control and don’t have the luxury of working with any type of html parsing so am stuck with regex
I can’t seem come up with a regex that would just match any whitespace inside any number of span tags.
Thanks
This could potentially be very slow with very large strings.
But this works:
With a replacement string of:
The reason I say it might be slow is that it’s having to find the whitespace (\s) and then search towards the left and to the right to see if it’s surrounded by a span tag. And it’ll have to do the same thing for every character of whitespace individually. But I believe this should work reliably as long as your HTML is well-formed and you aren’t dealing with nested span tags.
And by the way, since this is for .NET you can use Regex Hero to build the code for you: