How do I replace all line breaks in a string with
elements?

前端 未结 13 2538
刺人心
刺人心 2020-11-22 02:00

How can I read the line break from a value with JavaScript and replace all the line breaks with
elements?

Example:

A variable passe

13条回答
  •  没有蜡笔的小新
    2020-11-22 02:35

    This will turn all returns into HTML

    str = str.replace(/(?:\r\n|\r|\n)/g, '
    ');

    In case you wonder what ?: means. It is called a non-capturing group. It means that group of regex within the parentheses won't be saved in memory to be referenced later. You can check out these threads for more information:
    https://stackoverflow.com/a/11530881/5042169 https://stackoverflow.com/a/36524555/5042169

提交回复
热议问题