开发者

Adding a new line using javascript

开发者 https://www.devze.com 2023-03-26 19:13 出处:网络
Hi I have a html snippet that looks like *hi<br>*hello<br/>*test<br /> I want this to be preformatted using javascript as

Hi I have a html snippet that looks like *hi<br>*hello<br/>*test<br />

I want this to be preformatted using javascript as

*hi<br>
*开发者_运维技巧hello<br/>
*test<br />

Is there a regular expression and javascript to do this.


str = str.replace(/<br>/g,"<br />\n");

This means replace all new lines \n with a <br />


I believe this will help:

var str = '*hi<br>*hello<br/>*test<br />';
str.replace(new RegExp('<br\s*\\?>', 'i'), "$0\n");

That way you will keep diffrence between
,
and
.


Could you use replace() to replace * with *\n? You'd end up with an extra \n at the start but saves using regex.


If str is your HTML string

str = str.replace(/\n/g , "<br />");
0

精彩评论

暂无评论...
验证码 换一张
取 消