如何使用JQuery在标签内查找文本

时间:2018-12-01 08:36:08

标签: jquery

嗨,我有一个类似下面的段落

<p>The strength of the notion of the cultural biography, in my mind, is that it provides us with a way to escape from these preoccupations <xref>1990</xref>.  The algorithm takes a set of earthly 1989 biographies as input and produces a set of improved resurrection 1915 biographies as output.</p>

我需要在<p>标签中查找未加标签的年份。我尝试输入代码,请在下面检查

if($xml.find("p").length > 0)
{
    var $element = $xml.find("p").addBack("p");
    $element.each(function()
    {
        if($(this).clone().find('xref').remove().end().text().match(/19+[0-9][0-9]/))
        {
            //*****
        }                           

    });
}

但是此代码返回单个未加标签的年份,我希望在段落中完整填写年份列表

3 个答案:

答案 0 :(得分:1)

$(document).ready(function(){

var $container=$(this).find("xref").remove().text().match(/\d+/g);


   var num =$(this).find("#value").text().match(/\d+/g).join(",");
    console.log( num);  
         
});
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>


<p id="value">The strength of the notion of the cultural biography, in my mind, is that it provides us with a way to escape from these preoccupations <xref>1990</xref> The algorithm takes a set of earthly 1989 biographies as input and produces a set of improved resurrection 1915  biographies as output.</p>

答案 1 :(得分:0)

使用正则表达式将所有数字length of 4leading and trailing spaces匹配。

尝试以下解决方案

$(document).ready(function(){
     var regexp = /(\s+)\d{1,4}(\s+)/g;
     var txt = $('p').text();
     var match, matches = [];

     while ((match = regexp.exec(txt)) != null) {
       matches.push(parseInt(match[0].replace(/\s/g, '')));
     }

     console.log(matches);
  });
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>


<p>The strength of the notion of the cultural biography, in my mind, is that it provides us with a way to escape from these preoccupations <xref>1990</xref>.   The algorithm takes a set of earthly 1989 biographies as input and produces a set of improved resurrection 1915  biographies as output.</p>

答案 2 :(得分:0)

获取未标记的年份值

var untagged_19_20 = $(this).clone().find('xref').remove().end().text().match(/19+[0-9][0-9]/g);
var untagged_19_20 = untagged_19_20 + ',' + $(this).clone().find('xref').remove().end().text().match(/20+[0-9][0-9]/g);