user1917032
user1917032

Reputation: 169

Regex get text between first p tags

I'm trying to edit the Original code to get the first p tag from product.description and tried the New Code, but with no success. Is there another method anyone can recommend? Thanks in advance for your help.

Original Code:
if (quickview.find('.des').length > 0) {          
  var description = product.description.replace(/(<([^>]+)>)/ig, "");
  quickview.find('.des').text(description);
}

New Code:
if (quickview.find('.des').length > 0) {
    var description = product.description.match(/<p>(.*?)<\/p>/g, "");
    quickview.find('.des').text(description);
}

Example parse for New Code:

<p>First inner content needed.</p> <p>content...</p> <span>content...</span>

I'm trying to grab everything inside the first paragraph only and need to remove all p tags and any other tags after.

Upvotes: -1

Views: 844

Answers (1)

user1917032
user1917032

Reputation: 169

if (quickview.find('.des').length > 0) {                    
  var description = product.description.match(/<p class="desQuickView">(.*?)<\/p>/g, "").map(function(val){
       return val.replace(/(<([^>]+)>)/ig, "");
  });
  quickview.find('.des').text(description);

I found a way to target my first paragraph by adding a class name. Then I look for all tags and replaced them with an empty strings.

Upvotes: 0

Related Questions