0

I have string in variable (Javascript/jQuery) containing content like this:

Lorem ipsum dolor sit amet, consectetur adipiscing elit.
<p>Morbi a faucibus magna. Donec lacinia, leo eget</p>
Pellentesque aliquet luctus lobortis.
<p>Morbi a faucibus magna. Donec lacinia, leo eget</p>
 massa iaculis leo, nec auctor

how i can wrap all unwrapped content in p tags? So that string looks like:

<p>Lorem ipsum dolor sit amet, consectetur adipiscing elit.</p>
<p>Morbi a faucibus magna. Donec lacinia, leo eget</p>
<p>Pellentesque aliquet luctus lobortis.</p>
<p>Morbi a faucibus magna. Donec lacinia, leo eget</p>
<p>massa iaculis leo, nec auctor</p>

Thank you!

Domagoj
  • 55
  • 1
  • 7
  • Since this is a string of text, are there new line characters? How do you know when something should be wrapped? – Jason B Dec 04 '13 at 15:56
  • There is no new line characters, just one continuous string. Basically what needs to be wrapped is everything before first

    tag, everything after last

    tag, and everything between and

    (if exists any of that)..

    – Domagoj Dec 04 '13 at 16:00
  • @abdom: Okay. So where are you getting stuck? – T.J. Crowder Dec 04 '13 at 16:03
  • @T.J.Crowder I'm still learning JS so i didn't know how to write this, i was trying something with split and join but no luck.. This awnser from adeneo works great – Domagoj Dec 04 '13 at 16:17

2 Answers2

3

Something like

var str = 'your string';
var div = $('<div />', {html: str});

div.contents().filter(function() {
    return this.nodeType === 3;
}).wrap('<p />');

var new_str = div.html();

FIDDLE

Using a new jQuery object to parse the string as HTML, and then filtering out unwrapped textnodes, and wrapping them with paragraphs, and outputting the changed HTML as the new string.

adeneo
  • 312,895
  • 29
  • 395
  • 388
  • This works great! :) I'm still trying to figure out how it works as I'm new in JS :) Thank you!! – Domagoj Dec 04 '13 at 16:14
  • It works exactly like the added explanation says. It creates a jQuery object with the HTML as content, and then filters out textnodes and wraps them in paragraphs, and to get the altered HTML back it calls html() – adeneo Dec 04 '13 at 16:17
0

Here's a jQuery-free way to do it using only string methods (no DOM required.)

var text = "Lorem ipsum dolor sit amet, consectetur adipiscing elit.<p>Morbi a faucibus magna. Donec lacinia, leo eget</p>Pellentesque aliquet luctus lobortis.<p>Morbi a faucibus magna. Donec lacinia, leo eget</p>massa iaculis leo, nec auctor",
    unwrapped = text.split(/<p>\b[^>]*<\/p>/g), //regex to split on all p wrapped text
    i;

for (i=0; i < unwrapped.length; i++) {
    text = text.replace(unwrapped[i], '<p>' + unwrapped[i] + '</p>');
};
adamb
  • 4,815
  • 1
  • 25
  • 40
  • [**TH̘Ë͖́̉ ͠P̯͍̭O̚​N̐Y̡ H̸̡̪̯ͨ͊̽̅̾̎Ȩ̬̩̾͛ͪ̈́̀́͘ ̶̧̨̱̹̭̯ͧ̾ͬC̷̙̲̝͖ͭ̏ͥͮ͟Oͮ͏̮̪̝͍M̲̖͊̒ͪͩͬ̚̚͜Ȇ̴̟̟͙̞ͩ͌͝S̨̥̫͎̭ͯ̿̔̀ͅ**](http://stackoverflow.com/questions/1732348/regex-match-open-tags-except-xhtml-self-contained-tags) – adeneo Dec 04 '13 at 16:29
  • @adeneo I'm just exploiting a specific property of the posed text (the alternating of wrapped/unwrapped text), not descending into regex hell :P – adamb Dec 04 '13 at 16:51