0

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.

4
  • Err... why don't you read the documentation and find out for yourself? This is really basic stuff. Commented Aug 9, 2011 at 14:35
  • yes but I was having some problems. So, I just wanted to know the right way..thanks Commented Aug 9, 2011 at 14:45
  • Next time post the code you have so far. Commented Aug 9, 2011 at 14:51
  • So I have a comment....is the forum only for pros who know all things and know how to program a big app or is this for rookies also?? Since somebody downvoted and even matt pointed, can I get an idea of what to ask and what not to? Commented Aug 11, 2011 at 18:26

4 Answers 4

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

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

Sign up to request clarification or add additional context in comments.

1 Comment

Doesn't the OP want to add a new line after each BR element?
1

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
.

Comments

0

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

1 Comment

yeah, but that is not desired in a bigger picture
0

If str is your HTML string

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

1 Comment

I wanted a new line in place of a break, not the other way round...thx

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.