Remove tab ('\t') from string javascript

前端 未结 2 516
滥情空心
滥情空心 2021-02-04 01:08

How can I remove tab from a any string on javascript?

when I get my string it comes as a buffer like this:



        
2条回答
  •  伪装坚强ぢ
    2021-02-04 01:30

    The problem is probably in how you define content.

    If content=='\t session',

    `content=String(content).replace('\t','');`
    

    implies that content==' session'.

    On a side-note, the String(...) is unnecessary.

    `content=content.replace('\t','');`
    

    achieves the same result.

    Edit:

    String(array) does not work as you expect.

    You have to either perform the replace before you split the string or perform the replace on every element of the array separately.

    Instead of

    var content = data.toString().split('\r\n');
    content=String(content).replace('\t','');
    

    try

    var content = data.toString().replace('\t', '').split('\r\n');
    

    Note that replace('\t', '') will replace only the first occurrence of \t. To do a global replace, use the RegExp Alex K. suggested:

    var content = data.toString().replace(/\t/g, '').split('\r\n');
    

提交回复
热议问题