how to find text inside a tag using JQuery

jquery find text
jquery if element contains text
jquery replace text in div
jquery get span text
.text() javascript
jquery innerhtml
jquery find string
jquery selector

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

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

.text(), Description: Select all elements that contain the specified text. The matching text can appear directly within the selected element, in any of that As with attribute value selectors, text inside the parentheses of :contains() can be written as a  Answer: Use the jQuery text() method You can simply use the jQuery text() method to get all the text content inside an element. The text() method also return the text content of child elements.

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

:contains() Selector, text() method will get all the space and new line characters within the dom element. If you don't want it then you can use jQuery's trim method. In my jQuery function I read the div tag into variable. There are many text fields inside that div tag. I want to get specific field by its name. Tag: var guest_html=$('#guests_feilds1').html()

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>

how to find text inside div with class jquery, Definition and Usage. The text() method sets or returns the text content of the selected elements. When this method is used to return content, it returns the text  Stack Overflow for Teams is a private, secure spot for you and your coworkers to find and share information. Learn more jQuery append text inside of an existing paragraph tag

jQuery text() Method, curContent: It returns current content of selected elements. Example: This example gets the content by using JQuery's text() method . jQuery Insert Content. In this tutorial you will learn how to insert new elements or contents to the document using jQuery. jQuery Insert New Content. jQuery provides several methods, like append(), prepend(), html(), text(), before(), after(), wrap() etc. that allows us to insert new content inside an existing element.

JQuery, The :contains() is a jQuery selector which allows us to search text within the element and select it and perform an action on it. Parameter Description; filter: Required. A selector expression, element or jQuery object to filter the search for descendants Note: To return multiple descendants, separate each expression with a comma.

jQuery - Search text in the element with :contains() selector, This article shows how to search text inside a div using jQuery. font-family: Arial​; font-weight: bold;">Find Your Text</td>; </tr>; <tr>; <td></td>  Given a jQuery object that represents a set of DOM elements, the .find() method allows us to search through the descendants of these elements in the DOM tree and construct a new jQuery object from the matching elements. The .find() and .children() methods are similar, except that the latter only travels a single level down the DOM tree.

Comments
  • less jquery in the world please :)
  • thanks but it return tagged year too
  • 'var untagged_19_20 = untagged_19_20.replace(/( 19+[0-9][0-9])/g, '<span class="ref_highlght">$1</span>'); ' how to use replace here
  • @Swashi your solution only for the years that start with 19 and it is specific for your text only. while using this code will work for any number. best regards.
  • Thanks, But I need to find UNTAGGED Year example Output should be 1989, 1915
  • @Swashichanged logic accordingly. test pls