Replacing from javascript dom text node
This is much easier than you're making it. The text node will not have the literal string " "
in it, it'll have have the corresponding character with code 160.
function replaceNbsps(str) { var re = new RegExp(String.fromCharCode(160), "g"); return str.replace(re, " ");}textNode.nodeValue = replaceNbsps(textNode.nodeValue);
UPDATE
Even easier:
textNode.nodeValue = textNode.nodeValue.replace(/\u00a0/g, " ");
If you only need to replace
then you can use a far simpler regex:
var textWithNBSpaceReplaced = originalText.replace(/ /g, ' ');
Also, there is a typo in your div example, it says &nnbsp;
instead of
.
That first line is pretty messed up. It only needs to be:
var cleanText = text.replace(/\xA0/g,' ');
That should be all you need.