views:

75

answers:

2

How can I do this with the JS replace() method:

  • Make \n\n change to <p>$1</p>
  • Change single \n to <br>
  • Then back again. I think I have this part, see the JS at the bottom.

Example HTML:

<p>Hello</p><p>Wor<br>ld</p>

The <textarea> would look like:

Hello

Wor
ld

So, how can I achieve this? It's an AJAX form where when you click on this div it changes to a <textarea> and back, and fourth, etc. So, I need to it to go from <p>s and <br>s to \n\n and \n. For the going to <textarea> from HTML I have:

$(this).html().replace(/\s?<\/?(p|br\s?\/?)>\s?/g,"\n")

to Victor, and others,

I tried this code to convert it back, but it gave me this in return (the ... is just a lot more text)

$(this).html().replace(/\n/g, "<br>").replace(/<br><br>(.*)?/g, "<p>$1</p>");

gave me:

<div class="editable" data-name="notes-content" data-type="textarea">
“Time Certain” indicates that an item will not be heard by Council prior to the time certain
.<p>Communications items are three minutes each. ... 
<br><br>The * indicates an emergency ... 
<br><br>Check our Web site: www.portlandonline.com
<br>
</p>
</div>

If you notice, it didnt wrap the first line, and its not wrapping them in <p>s, just the entire thing, i need it all in <p>s

A: 

Here you go:

$(this).html().replace(/\n/g, "<br>").replace(/<br><br>(.*)?/g, "<p>$1</p>");
Vincent
Ah, duh! genius! Let me try this out, looks right, and ill let you know :) thanks!
Oscar Godson
Damn, doesnt look like that works :\ see above!
Oscar Godson
+1  A: 

How about this (version #4):

$(this).html().replace(/\n/g, "<br>").replace(/(.+?)<br><br>/g, "<p>$1</p>");

serg
Not sure why its doing this, but its converting all the paragraphs to be <p>$0</p>? any idea why?
Oscar Godson
Sorry I didn't test it. I've edited the answer (I assumed $0 would return the whole match)
serg
Really close, thanks! but. its giving me: “Time Certain”<br><p>indicates that an item will not be heard by Council prior to the time certain.<br><br></p> (time certain, or, the first line is never wrapped, <br>s translate fine, and each paragraph is wrapped fine! perfect.) The only problem is the first line isn't wrapped, when it was <p>time certain<br>...</p> and it adds 2 <br> before the </p> everytime, and if you keep clicking on and off they stack up like <br><br><br><br><br><br></p> :\. Other than the <br> issues and the first line, its perfect.
Oscar Godson
Please try a new version, I've tested it this time. The problem was that js doesn't match \n if you use dot notation, which took me a while to figure out.
serg
1 Weird thing:http://www.portlandonline.com/videogets translated to:http://www.portlandonline.com/<p>video</p>Weird?And, now nothing is being wrapped in <p>s :(Its just "content, content and some content<br><br>More content, some more here<br><br>" etc ...?Again, thanks SO much for this.
Oscar Godson
Try #4, hopefully final one :)
serg
OK OK! SUPER SUPER CLOSE! You kick ass. It does it perfect, EXCEPT the last line. Everything, including <p>s and <br>s are translated perfectly, 1 final thing:<p>City Council meetings can be viewed at anytime, on demand, via the Internet at http://www.portlandonline.com/video</p>Check our Web site: www.portlandonline.com<br>Notice the last line does NOT have a <p> wrapping it, which it should, but has a <br>... is that due to MY conversion, or can you fix it with this regex?
Oscar Godson
But does your text end with double `\n\n`? That's what triggers `<p></p>`. If you need last sentence to be always wrapped with p then just add `\n\n` to the end of your text before doing this replacing.
serg
Thank you so much! My final code, after your #4 was: .replace(/\n$/,'') when it converted TO the textarea and then i added: $(this).val($(this).val()+'\n\n'); to the value of the textarea when it was going FROM textarea TO div. Once again, THANK YOU
Oscar Godson
You are welcome, glad to help :)
serg