最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

node.js - Trimming n, s, t from javascript string - Stack Overflow

programmeradmin4浏览0评论

I have a jQuery selector which gets the html within a selector, like so:

$('.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, \n and \t characters disappear. I'm currently using nodeJS' validate module which has a special sanitize(string).trim() command which should trim away white space. From the docs:

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.

Any ideas on what I'm missing, or what I can do to get this working?

I have a jQuery selector which gets the html within a selector, like so:

$('.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, \n and \t characters disappear. I'm currently using nodeJS' validate module which has a special sanitize(string).trim() command which should trim away white space. From the docs:

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.

Any ideas on what I'm missing, or what I can do to get this working?

Share Improve this question asked Aug 19, 2013 at 7:05 JVGJVG 21.2k48 gold badges140 silver badges215 bronze badges 3
  • Well, the whitespace is not at the beginning nor the end so trim won't do anything, and the regex you tried is a string not a regex, so that won't do anything either... – elclanrs Commented Aug 19, 2013 at 7:07
  • @rps That's not working for me. – JVG Commented Aug 19, 2013 at 7:22
  • @Jascination Notice, that .replace() just returns a replaced string. You need to do str = str.replace(...). – Teemu Commented Aug 19, 2013 at 7:28
Add a comment  | 

1 Answer 1

Reset to default 26

http://jsfiddle.net/HkJbr/....

.replace(/[\n\t\r]/g,"")

发布评论

评论列表(0)

  1. 暂无评论