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

前端 未结 13 2315
刺人心
刺人心 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:22

    For those of you who just want to allow max. 2
    in a row, you can use this:

    let text = text.replace(/(\r?\n){2,}/g, '

    '); text = text.replace(/(\r?\n)/g, '
    ');

    First line: Search for \n OR \r\n where at least 2 of them are in a row, e.g. \n\n\n\n. Then replace it with 2 br

    Second line: Search for all single \r\n or \n and replace them with

提交回复
热议问题