2

I have pages with HTML that look like this:

<div class="row">
    <div class="col-md-12">
        <h2>Some Title</h2>
        Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua.
        <p>Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua.</p>
    </div>
</div>

Now I need to have the first Lorem ipsum text wrapped in <p></p> tags aswell. Is there a way to detect this and do it with regex? I would be using regex to make these changes directly in the database.

vsync
  • 118,978
  • 58
  • 307
  • 400

2 Answers2

1

Since your structure is fixed it's just a matter of

  1. Finding the first non-empty TextNode
  2. Creating a p element
  3. Substituting the text node with the paragraph inside the parent node.

I made the paragraphs green so you can see the effect. Just click the "Wrap" button to see it in action.

function wrap() {
  let div = document.querySelector('div.row>div.col-md-12');    // get the container
  for (let i = 0; i < div.childNodes.length; i++) {             // loop through children
    let n = div.childNodes[i];
    if (n.nodeName == "#text" && n.textContent.trim() !== '') { // children found!
      let p = document.createElement('p');                      // create a `p` element
      p.textContent = n.textContent;                            // put the original text inside
      n.parentNode.replaceChild(p, n);                          // swap!
      break;                                                    // we're done here
    }
  }
}
p {
  background-color: green;
}
<div class="row">
  <div class="col-md-12">
    <h2>Some Title</h2>
    Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua.
    <p>Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua.</p>
  </div>
</div>
<button onclick="javascript:wrap()">Wrap</button>
Federico klez Culloca
  • 26,308
  • 17
  • 56
  • 95
0

I used regex search, and when find the string, make a child element of class col-md-12 and removed another string without elements!

<html>
    <body>
        <div class="row">
            <div class="col-md-12">
                <h2>Some Title</h2>
                Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua.
            </div>
        </div>
    </body>
    <script>

        var string = document.getElementsByTagName('html')[0].innerHTML;

        var regex = /Lorem ipsum[a-zA-Z0-9_ _,_.]*/g;

        var save = regex.exec(string)[0];
        document.getElementsByTagName('html')[0].innerHTML = document.getElementsByTagName('html')[0].innerHTML.replace(save, "");
        var first = document.createElement("p");
        var text = document.createTextNode(save);
        first.appendChild(text);

        document.getElementsByClassName("col-md-12")[0].appendChild(first);

    </script>
</html>
deon cagadoes
  • 582
  • 2
  • 13