Skip to content Skip to sidebar Skip to footer

Js - Conversion Of Non Breakable Space  

I'm reading out a text from an HTML element and saving it in an JS array, but if the user puts in more then one empty space it is converted into a non breakable space symbol. Later

Solution 1:

Regex notation table for whitespace

\x20 – standard space ‘\s’
\xC2\xA0 – ‘ ’
\x0D -  ‘\r’
\x0A – new Line or ‘\n’
\x09 – tab or ‘\t’ 

JS table notation :

String.fromCharCode(160) -  

Now use it for replacing all your characters, add more if you want to remove newline or carriage return

var re = '/(\xC2\xA0/| )';
x = x.replace(re, ' ');

you can also use

var re = '/(\xC2\xA0/| )';
x = x.replace(re, String.fromCharCode(160));

Try this tool to test more Regular expression www.rubular.com

Post a Comment for "Js - Conversion Of Non Breakable Space  "