JS - non-destructible space transform & nbsp

I am reading text from an HTML element and storing it in a JS array, but if the user places more than one empty space, it is converted to a character without splitting the space. Later, if I read the array, it produces "& nbsp" instead of empty space. How can I detect empty space?

&nbsp  //instead of an empty space

I tried replacing, but it does not work:

myText.replace(/( )*/g," ")
+1
source share
1 answer

Regular expression notation table for spaces

\x20 โ€“ standard space โ€˜\sโ€™
\xC2\xA0 โ€“ โ€˜ โ€™
\x0D -  โ€˜\rโ€™
\x0A โ€“ new Line or โ€˜\nโ€™
\x09 โ€“ tab or โ€˜\tโ€™ 

JS table designation:

String.fromCharCode(160) -  

Now use it to replace all your characters, add more if you want to delete a new line 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));

www.rubular.com

+3

Source: https://habr.com/ru/post/1616132/


All Articles