Copy link to clipboard
Copied
<html> <div><p> Hi there</p> </div> </html>
Sample code above has whitespace, which I need to get rid of. I've found various examples used in different code settings and tried plugging them into a CF REReplace() function, but nothing has worked so far.
e.g. something like this in node
var result = html.replace(/>\s+|\s+</g, function(m) {
return m.trim();
});
Any help greatly appreciated.
You could solve it in a simple way, by using reReplaceNocase() twice. The first call replaces ">\s" with ">", the second replaces "\s<" with "<":
<cfscript>
html = "<html> <div><p> Hi there</p> </div> </html>";
htmlSpaceless = html.reReplaceNoCase(">\s",">","all").reReplaceNoCase("\s<","<","all");
writeoutput(htmlSpaceless);
</cfscript>
var result = html.rereplace("\s*([<|>])\s*", "\1", "all");
Copy link to clipboard
Copied
You could solve it in a simple way, by using reReplaceNocase() twice. The first call replaces ">\s" with ">", the second replaces "\s<" with "<":
<cfscript>
html = "<html> <div><p> Hi there</p> </div> </html>";
htmlSpaceless = html.reReplaceNoCase(">\s",">","all").reReplaceNoCase("\s<","<","all");
writeoutput(htmlSpaceless);
</cfscript>
Copy link to clipboard
Copied
Yes, that is perfect. Thank you so much!
Copy link to clipboard
Copied
My pleasure, @paul_8809 !
Copy link to clipboard
Copied
var result = html.rereplace("\s*([<|>])\s*", "\1", "all");
Copy link to clipboard
Copied
Good find, @kazu98296633 . Your suggestion solves a wider problem of which @paul_8809 's is a part. Namely, it removes any spaces before or after the characters "<" and ">" :
<cfscript>
html = "<html> < div > < p > Hi there < /p > < /div > < /html >";
htmlSpaceless = html.rereplace("\s*([<|>])\s*", "\1", "all");
writeoutput(htmlSpaceless);
</cfscript>