Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8664341
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 12, 20262026-06-12T17:13:06+00:00 2026-06-12T17:13:06+00:00

I need to extract the text from the nodes in an html file and

  • 0

I need to extract the text from the nodes in an html file and I’m trying to use XPath and Javascript.

The required condition is that the text must contain an specific word.

Let’s take by example the next html file:

<html>
    <body>
        <p>
            Hi, try to extract the word username here and here <b>username</b>
        </p>
    </body>
</html>

And try to get the text from text nodes containing the word ‘username’ with this expression:

var search = document.evaluate('//*[contains(child::text(), \"username\")]/child::text()', document, null, XPathResult.ANY_TYPE, null);

Iterating through search I’ve found the desired result but unwanted objects too:

["Hi, try to extract the word username here and here", Text, "username"]

where Text is an Object whose textContent is only the carriage return symbol (I’m using Google Chrome console). Where does this object come from?

Can anyone, please, give a more precise XPath expression that excludes those Objects or should I exclude them in my code?

The ideal search should be:

["Hi, try to extract the word username here and here", "username"]

Thanks everybody!

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-12T17:13:08+00:00Added an answer on June 12, 2026 at 5:13 pm

    Looks like you want

    var search = document.evaluate('//text()[contains(., \"username\")]',
       document, null, XPathResult.ANY_TYPE, null);
    

    (I’m not sure why you’re escaping your double quotes inside single quotes, but that’s a separate issue.)

    Your existing code,

    var search = document.evaluate('//*[contains(child::text(), \"username\")]/
             child::text()', document, null, XPathResult.ANY_TYPE, null);
    

    says,

    • (1) Find all elements that have text node children that contain “username”.
    • (2) Now return all the text node children of those elements (regardless of what they contain).

    The (1) part of the expression will return element nodes <p> and <b>.

    For the (2) step, <b> has only one text node child, but <p> has two: the one before the <b> (which contains “username”) and the one after the <b> (which contains only whitespace).

    Solution: Forget about elements — they are an irrelevant distraction. Just select the desired text nodes directly.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I need to extract text from an HTML file using C#. I am trying
I use Jericho HTML Parser 3.1. I need to extract text from html, handle
I have a space delimited text file, from which I need to extract individual
I have a string of text that contains html, and I need to extract
I need a library that can extract text from handwritten paper once I scan
I need to use a regexp to extract bold text from rtf formatted text.
I'm trying to extract a portion of text from a string. I need to
I need to extract the text from a PDF that has already been transformed
I need to extract some text from an SWF file. It is possible? Are
I need to extract some text from a string, then replace that text with

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.