简体   繁体   中英

select multiple line breaks regex

So what I am trying to do is replace one or more instances of newline with the br tag in javascript. So far I have:

description.replace(/\n/g, '<br />');

However if there is a case where there are 2/3 newlines's in a row I get 2/3 br tags. Is there a way in regex to say give me any instances of one or more newlines's in a row and replace that whole thing with one br tag so that even if I have:

\n\n\n\n\n\n\n\n\n

that would get replace with just:

<br >

You can add the + quantifier to indicate one or more matches.

description.replace(/\n+/g, '<br />');

PS: you need to read more about regular expressions, this was fairly straight forward.

description.replace(/\n+/g, '<br />');

快乐的作业

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM