How to remove all line breaks from a string

前端 未结 16 1563
轮回少年
轮回少年 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:15

    USE THIS FUNCTION BELOW AND MAKE YOUR LIFE EASY

    The easiest approach is using regular expressions to detect and replace newlines in the string. In this case, we use replace function along with string to replace with, which in our case is an empty string.

    function remove_linebreaks( var message ) {
        return message.replace( /[\r\n]+/gm, "" );
    }
    

    In the above expression, g and m are for global and multiline flags

提交回复
热议问题