Remove non breaking space ( ) from between el

2019-02-21 02:31发布

问题:

How can I use jquery to remove non-breaking spaces (nbsp) which appear between html elements?

So, for example, the following code is generated by a cms:

<div><span>content</span>&nbsp;<span>content</span></div>

I am able to target the elements using slectors.

The following page suggests an answer to remove normal whitespace, but not non-breaking spaces. Remove whitespace and line breaks between HTML elements using jQuery

Thanks

回答1:

var div = "<div><span>content</span>&nbsp;<span>content</span></div>";    
var newdiv = div.replace(/&nbsp;/g,'');

demo