首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >如何使用JQuery在标记中查找文本

如何使用JQuery在标记中查找文本
EN

Stack Overflow用户
提问于 2018-12-01 08:36:08
回答 3查看 115关注 0票数 0

嗨,我有一段如下所示

代码语言:javascript
复制
<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>标签中找到无标记的年份。我试一试代码,请检查下面

代码语言:javascript
复制
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]/))
        {
            //*****
        }                           

    });
}

但是这段代码返回单个无标记年份,我想要一段完整的年份列表。

EN

回答 3

Stack Overflow用户

回答已采纳

发布于 2018-12-01 09:35:21

代码语言:javascript
复制
$(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);  
         
});
代码语言:javascript
复制
<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
EN

Stack Overflow用户

发布于 2018-12-01 08:43:54

使用regex将所有数字length of 4leading and trailing spaces匹配。

尝试下面的解决方案

代码语言:javascript
复制
$(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);
  });
代码语言:javascript
复制
<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>

票数 0
EN

Stack Overflow用户

发布于 2018-12-01 10:18:51

获取未标记的年份值。

代码语言:javascript
复制
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);
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/53569109

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档