Remove tab ('\t') from string javascript

前端 未结 2 785
长发绾君心
长发绾君心 2021-02-04 00:46

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:32

    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');
    

提交回复
热议问题