JavaScript / jQuery从节点的text()值中删除字符160 - Regex [英] JavaScript/jQuery removing character 160 from a node's text() value - Regex
问题描述
$('#customerAddress').text().replace(/\xA0/,"").replace(/\s+/," ");
追踪范围中的值(id = customerAddress),我想减少所有部分空格到单个空格。 / \s + /可以工作,除了这个应用程序在街道地址和州/邮编之间获得160个字符
有什么更好的方式来写这个?这目前无效。
Going after the value in a span (id=customerAddress) and I'd like to reduce all sections of whitespace to a single whitespace. The /\s+/ whould work except this app gets some character 160's between street address and state/zip What is a better way to write this? this does not currently work.
更新:
我已经发现
UPDATE: I have figured out that
$('.customerAddress').text().replace(/\s+/g," ");
清除160s和空格。
但是我怎么写一个正则表达式只是在160年代之后?
clears the 160s and the spaces. But how would I write a regex to just go after the 160s?
$('.customerAddress').text().replace(String.fromCharCode(160)," ");
甚至没有工作。
注意:我正在使用Firefox / Firebug进行测试
Note: I'm testing in Firefox / Firebug
推荐答案
\s
does already contain the character U+00A0:
[\t\n\v\f\r \u00a0\u2000\u2001\u2002\u2003\u2004\u2005\u2006\u2007\u2008\u2009\u200a\u200b\u2028\u2029\u3000]
但你应该添加 g 修饰符来全局替换:
But you should add the g modifier to replace globally:
$('#customerAddress').text().replace(/\s+/g, " ")
否则只会替换第一场比赛。
Otherwise only the first match will be replaced.
这篇关于JavaScript / jQuery从节点的text()值中删除字符160 - Regex的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!