I have HTML coming from templates that I need to remove spaces between elements and new lines. I don't want to remove spaces that would break the HTML, so it is becoming tricky for me. So far I have the below to remove new lines, but spaces I am stuck on:
'<div id="head"></div> \n <div id="body"></div> <div id="foot"></div> '.replace(/(\r\n|\n|\r)/gm, '');
Is there hopefully something built into jQuery instead of a complex regex maybe to remove the spaces?
This should work :
.replace(/^\s+|\r\n|\n|\r|(>)\s+(<)|\s+$/gm, '$1$2')
Fiddle
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