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.

6
  • 3
    Use an HTML parser! Regex can't parse HTML correctly. Commented Jan 2, 2010 at 11:33
  • You missed this post: stackoverflow.com/questions/1732348/… Commented Jan 2, 2010 at 11:34
  • What programming language are you using? The answers can be a lot more relevant for you if you tell us. Commented Jan 2, 2010 at 11:34
  • I'm using Javascript on Google Chrome. I need to filter a valid HTML code in someway. Commented Jan 2, 2010 at 12:41
  • 1
    @Licx: Is you HTML already a DOM or just source code? If the former, you really should use DOM methods. Commented Jan 2, 2010 at 14:02

2 Answers 2

2

Regular expressions do allow a non-trivial form of negation through lookahead but in this case it would be just good as an excercise because, while I'n no zealot that burns with holy fire every time regexp get mentioned together with HTML, this is really a problem to be solved with a parser.

Sign up to request clarification or add additional context in comments.

Comments

0

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.