嗨,我有一段如下所示
<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]/))
{
//*****
}
});
}但是这段代码返回单个无标记年份,我想要一段完整的年份列表。
发布于 2018-12-01 09:35:21
$(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>
发布于 2018-12-01 08:43:54
使用regex将所有数字length of 4与leading 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>
发布于 2018-12-01 10:18:51
获取未标记的年份值。
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);https://stackoverflow.com/questions/53569109
复制相似问题