简体   繁体   English

正则表达式获取第一个 p 标签之间的文本

[英]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.我正在尝试编辑原始代码以从 product.description 获取第一个 p 标签并尝试使用新代码,但没有成功。 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.我试图只抓取第一段内的所有内容,并且需要删除所有 p 标签和之后的任何其他标签。

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.我找到了一种通过添加 class 名称来定位我的第一段的方法。 Then I look for all tags and replaced them with an empty strings.然后我查找所有标签并将它们替换为空字符串。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM