Swashi
Swashi

Reputation: 59

how to find text inside a tag using JQuery

Hi I have a paragraph like below

<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>

I need to find untagged year in a <p> Tag. I try a code please check below

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]/))
        {
            //*****
        }                           

    });
}

But this code return single untagged year, I want complete year list in a paragraph

Upvotes: 0

Views: 134

Answers (3)

Shiv Kumar Baghel
Shiv Kumar Baghel

Reputation: 2480

using regex for matching all numbers length of 4 with leading and trailing spaces.

try below solution

$(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>

Upvotes: 0

I_Al-thamary
I_Al-thamary

Reputation: 4043

$(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>

Upvotes: 1

Swashi
Swashi

Reputation: 59

Get the untagged year values

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);

Upvotes: 0

Related Questions