简体   繁体   中英

Replacing spaces between html tags with nbsps

I want to replace spaces between html tags with nbsps using pure JavaScript. This is my html:

<div><span>Apple</span>     <span>Grapes</span></div>

You can see spaces between 2 span nodes. These spaces should be replaced by &nbsps.

Result should be:

   <div><span>Apple</span><span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span>Grapes</span></div>

Please help me.

Try this simple logic

 var input= "<div><span>Apple</span> <span>Grapes</span></div>" var output = input.replace( /<\\/span>\\s*<span>/g, function(match){ return match.replace(/\\s/g, "&nbsp;") } ); console.log( output );

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM