【发布时间】:2013-08-19 07:05:05
【问题描述】:
我有一个 jQuery 选择器,它在选择器中获取 html,如下所示:
$('.pp-product-description .noindent').html()
这是一个输出示例,我已经验证它是一个字符串:
<li class="standardBulletText">600 fill power down adds warmth without weight</li>
\r\n\t\t\t\t\t\t \t
<li class="standardBulletText">Matte shell with DriOff™ finish for water-resistant protection</li>\
r\n\t\t\t\t\t\t \t
<li class="standardBulletText">Snap-off, adjustable hood with removable faux fur trim</li>
\r\n\t\t\t\t\t\t \t<li class="standardBulletText">Hidden zip/snap closure except for top and bottom snaps</li>
我正在尝试将其缩小,以使 \r、\n 和 \t 字符消失。我目前正在使用nodeJS'validate module,它有一个特殊的sanitize(string).trim() 命令,应该修剪掉空白。来自文档:
var str = sanitize(' \t\r hello \n').trim(); //'hello'
这在过去对我有用,但现在不是。我也试过javascript内置的trim()方法,还有str.replace("\s+", "");,都没有用。
关于我缺少什么,或者我可以做些什么来让它发挥作用的任何想法?
【问题讨论】:
-
好吧,空格既不在开头也不在结尾,所以
trim不会做任何事情,而您尝试的正则表达式是字符串而不是正则表达式,所以也不会做任何事情... -
@rps 这对我不起作用。
-
检查我的答案@Jascination
-
@Jascination 注意,
.replace()只是返回一个替换的字符串。你需要做str = str.replace(...)。
标签: javascript node.js trim