从 javascript 字符串中修剪 \n、\s、\t
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/18308148/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Trimming \n, \s, \t from javascript string
提问by JVG
I have a jQuery selector which gets the html
within a selector, like so:
我有一个 jQuery 选择器,它在选择器中获取html
,如下所示:
$('.pp-product-description .noindent').html()
Here's an example of the output, I've verified that it's a string:
这是输出的示例,我已经验证它是一个字符串:
<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>
I'm trying to trim this down so that the \r
, \n
and \t
characters disappear. I'm currently using nodeJS
' validate
modulewhich has a special sanitize(string).trim()
command which should trim away white space. From the docs:
我正在尝试减少它,以便\r
,\n
和\t
字符消失。我目前正在使用nodeJS
'validate
模块,它有一个特殊的sanitize(string).trim()
命令,可以去除空白。从文档:
var str = sanitize(' \t\r hello \n').trim(); //'hello'
This has worked for me in the past, but here it isn't. I've also tried javascript's built in trim()
method, as well as str.replace("\s+", "");
, none are working.
这在过去对我有用,但在这里不是。我也试过 javascript 的内置trim()
方法,以及str.replace("\s+", "");
,都没有工作。
Any ideas on what I'm missing, or what I can do to get this working?
关于我缺少什么的任何想法,或者我可以做些什么来使它工作?
回答by
http://jsfiddle.net/HkJbr/....
http://jsfiddle.net/HkJbr/....
.replace(/[\n\t\r]/g,"")
.replace(/[\n\t\r]/g,"")