How to remove all line breaks from a string

前端 未结 16 1284
轮回少年
轮回少年 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条回答
  •  感情败类
    2020-11-22 12:19

    You can use \n in a regex for newlines, and \r for carriage returns.

    var str2 = str.replace(/\n|\r/g, "");
    

    Different operating systems use different line endings, with varying mixtures of \n and \r. This regex will replace them all.

提交回复
热议问题