theshadowmonkey
theshadowmonkey

Reputation: 679

Adding a new line using javascript

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.

Upvotes: 0

Views: 999

Answers (4)

tskuzzy
tskuzzy

Reputation: 36476

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

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

Upvotes: 1

Witold Sosnowski
Witold Sosnowski

Reputation: 506

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
.

Upvotes: 1

Nivas
Nivas

Reputation: 18364

If str is your HTML string

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

Upvotes: 0

Ben Vereen
Ben Vereen

Reputation: 51

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

Upvotes: 0

Related Questions