How to remove all line breaks from a string

前端 未结 16 1470
轮回少年
轮回少年 2020-11-22 11:36

I have a text in a textarea and I read it out using the .value attribute.

Now I would like to remove all linebreaks (the character that is produced when you press

16条回答
  •  Happy的楠姐
    2020-11-22 12:22

    Try the following code. It works on all platforms.

    var break_for_winDOS = 'test\r\nwith\r\nline\r\nbreaks';
    var break_for_linux = 'test\nwith\nline\nbreaks';
    var break_for_older_mac = 'test\rwith\rline\rbreaks';
    
    break_for_winDOS.replace(/(\r?\n|\r)/gm, ' ');
    //output
    'test with line breaks'
    
    break_for_linux.replace(/(\r?\n|\r)/gm, ' ');
    //output
    'test with line breaks'
    
    break_for_older_mac.replace(/(\r?\n|\r)/gm, ' ');
    // Output
    'test with line breaks'
    

提交回复
热议问题