从 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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-27 11:25:26  来源:igfitidea点击:

Trimming \n, \s, \t from javascript string

javascriptnode.jstrim

提问by JVG

I have a jQuery selector which gets the htmlwithin 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&trade; 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, \nand \tcharacters disappear. I'm currently using nodeJS' validatemodulewhich 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,"")