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 518333
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 13, 20262026-05-13T07:55:35+00:00 2026-05-13T07:55:35+00:00

I would like to extract text from an html document keeping the links inside

  • 0

I would like to extract text from an html document keeping the links inside it. for example:

From this HTML code

<div class="CssClass21">bla1 bla1 bla1 <a href="http://www.ibrii.com">go to ibrii</a> bla2 bla2 bla2 <img src="http://www.contoso.com/hello.jpg"> <span class="cssClass34">hello hello</span>

I would like to extract just this

bla1 bla1 bla1 <a href="http://www.ibrii.com">go to ibrii</a> bla2 bla2 bla2 hello hello

In another post on StackOverflow i have found the RegEx <[^>]*> which allows to extract text by replacing every match with nothing. How can I exclude the anchor tags from the match? It seems that RegEx do not allow inverse matching.

  • 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-05-13T07:55:36+00:00Added an answer on May 13, 2026 at 7:55 am

    Temporarily encode <a href ...>...</a> into something else, remove all other tags then restore the <a> tags:

    // Example in javascript:
    string.
        replace(/<a(.*?)>/g,'\0$1\0').
        replace(/<\/a>/,'\1').
        replace(/<[^>]*>/,'').
        replace(/\0(.*?)\0/,'<a$1>').
        replace(/\1/,'</a>');
    

    In the code above I use the NUL and SOH characters (ASCII 0x00 and 0x01) as replacements for <a> tags simply because it is highly unlikely that they would appear in strings. Feel free to replace them with any other character or sequence of characters that would not appear in your string.

    From additional comments it appears you’re operating in a browser. In which case the browser has already parsed the HTML for you into a nice DOM tree. Use DOM methods to parse through the tree and process it the way you want:

    function simpleHTML (domNode) {
        var ret = "";
        if (domNode.nodeType === Node.ELEMENT_NODE) {
            var children = domNode.childNodes;
            for (var i=0;i<children.length;i++) {
                var child = children[i];
    
                // Filter out unwanted nodes to speed up processing.
                // For example, you can ignore 'SCRIPT' nodes etc.
                if (child.nodeName != 'SCRIPT') {
                    if (child.nodeName == 'A') {
                        ret += '<a href="' + child.href + '">' +
                                   simpleHTML(child) +
                               '</a>';
                    }
                    else {
                        ret += simpleHTML(child);
                    }
                }
            }
        }
        else if (domNode.nodeType === Node.TEXT_NODE) {
            ret +=  domNode.nodeValue;
        }
        return ret;
    }
    // serialize the whole document:
    var simpleDocument = simpleHTML(document.body);
    
    // serialize a div:
    var simpleDiv = simpleHTML(document.getElementById('some_div'));
    
    // filter a html formatted string:
    var temp = document.createElement('DIV');
    temp.innerHTML = original_string;
    simple_string = simpleHTML(temp);
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I would like to extract items from this sample html, more specificly, i would
I would like to extract static text from between HTML tags: <p> text here
I would like to extract all text from subnodes of a specific document, AND
I need to extract all links from a html document having text as the
I would like to extract from a general HTML page, all the text (displayed
I would like to extract some text from an html file using Regex. I
I would like to parse a html page and extract the meaningful text from
Hello! I would like to extract all citations from a text. Additionally, the name
I'm using Windows, and I would like to extract certain columns from a text
I'd like to extract the text from an HTML file using Python. I want

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.