Remove tab ('\t') from string javascript

前端 未结 2 520
滥情空心
滥情空心 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:23

    You need a regexp to replace all occurrences;

    content = content.replace(/\t/g, '');
    

    (g being the global flag)

    /^\t+/ restricts to replacing leading tabs only, /^\s+/ includes any leading whitespace which is what you would need for "\t\t var" -> "var"

    Update

    You haven't said how the buffer is received & what type it is, my closest guess although its a strange thing to be receiving;

    var test_buffer_array = "\x0d \x0a \x3c \x25 \x72 \x65 \x73 \x70 \x6f \x6e \x73 \x65 \x2e \x73 \x74 \x61 \x74 \x75 \x73 \x20".split(" ")
    
    translate(test_buffer_array);
    
    function translate(data) {
        var content = data.join("").replace(/^\t+/gm, '');
        print(content);
    }
    
    result: "<%response.status"
    

提交回复
热议问题