JS - 不可破坏空间的转换& nbsp [英] JS - conversion of non breakable space &nbsp
问题描述
我从HTML元素中读出文本并将其保存在JS数组中,但如果用户放入多个空白空间,它将转换为不可破坏的空间符号。稍后如果我读出数组,它会打印出& nbsp而不是空白。如何检测空白空间?
& nbsp //而不是空白空间
我尝试了替换,但它不起作用:
myText.replace(/(& nbsp;)* / g,)
\x20 - 标准空间'\ ''
\xC2\xA0 - '& nbsp;'
\x0D - '\r'
\x0A - 新行或'\\\
'
\x09 - tab或'\ t'
JS表格符号:
String.fromCharCode(160) - & nbsp;
现在用它来替换你所有的角色,如果你想删除换行符或回车符, / p>
var re ='/(\xC2\xA0 / |& nbsp;)';
x = x.replace(re,'');
您也可以使用
var re ='/(\xC2\xA0 / |& nbsp;)';
x = x.replace(re,String.fromCharCode(160));
试试这个工具来测试更多正则表达式www.rubular.com
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 on if I read out the array it prints out " " instead of an empty space. How can I detect an empty space?
  //instead of an empty space
I tried replace, but it does not work:
myText.replace(/( )*/g," ")
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
这篇关于JS - 不可破坏空间的转换& nbsp的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!