How to remove extra white spaces using javascript or jquery?

后端 未结 4 2098
粉色の甜心
粉色の甜心 2021-02-05 04:16

I got HTML element contains this:

    
P6C245RO
4条回答
  •  挽巷
    挽巷 (楼主)
    2021-02-05 04:57

    You can remove all instances of congruent whitespace and newlines like so

    // Note: console.log() requires Firebug
    
    var str = '    this  is \n    some text \r don\'t \t you    know?   ';
    console.log( str );
    
    str = str.replace( /[\s\n\r]+/g, ' ' );
    console.log( str );
    

    Then to clean it up and apply it to your jQuery

    $.trim( $element.text().replace( /[\s\n\r]+/g, ' ' ) )
    

提交回复
热议问题