Replacing tab characters in JavaScript

前端 未结 3 1239
说谎
说谎 2020-12-08 14:58

Please consider the following HTML

 element:

This is some  
example code which    
     contains tabs         


        
3条回答
  •  孤城傲影
    2020-12-08 15:38

    It removes line breaks, extra spaces and line breaks:

    function removeNewlines(str) {
    //remove line breaks from str
    str = str.replace(/\s{2,}/g, ' ');
    str = str.replace(/\t/g, ' ');
    str = str.toString().trim().replace(/(\r\n|\n|\r)/g,"");
    console.log(str);
    }
    

    Demo:

    function removeNewlines(str) {
    //remove line breaks from str
    str = str.replace(/\s{2,}/g, ' ');
    str = str.replace(/\t/g, ' ');
    str = str.toString().trim().replace(/(\r\n|\n|\r)/g,"");
      console.log(str);
    }
    
    $('#acceptString').click(function() {
        var str = prompt('enter string','');
        if(str)
            removeNewlines(str)
    });
    
    

提交回复
热议问题