Exit
  • Global community
    • Language:
      • Deutsch
      • English
      • Español
      • Français
      • Português
  • 日本語コミュニティ
    Dedicated community for Japanese speakers
  • 한국 커뮤니티
    Dedicated community for Korean speakers
0

Regex for removing whitespace between html tags

Community Beginner ,
Mar 05, 2025 Mar 05, 2025
<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.

 

 

138
Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines

correct answers 2 Correct answers

Community Expert , Mar 05, 2025 Mar 05, 2025

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>
Translate
Community Beginner , Mar 05, 2025 Mar 05, 2025
var result = html.rereplace("\s*([<|>])\s*", "\1", "all");
Translate
Community Expert ,
Mar 05, 2025 Mar 05, 2025

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>
Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Beginner ,
Mar 05, 2025 Mar 05, 2025

Yes, that is perfect. Thank you so much!

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
Mar 05, 2025 Mar 05, 2025

My pleasure, @paul_8809 !

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Beginner ,
Mar 05, 2025 Mar 05, 2025
var result = html.rereplace("\s*([<|>])\s*", "\1", "all");
Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
Mar 06, 2025 Mar 06, 2025
LATEST

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>

 

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Resources