且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

使用正则表达式从HTML中提取文本和链接

更新时间:2022-11-14 20:55:38

临时编码< a href ... > ...< / a> ,移除所有其他标签,然后恢复< a> 标签:

  //在javascript中的示例:
字符串。
替换(/< a(。*?)> / g,'\0 $ 1 \ 0')。
替换(/< \ / a> /,'\1')。
替换(/< [>] *> /,'')。
替换(/ \0(。*?)\0 /,'< a $ 1>')。
replace(/ \ 1 /,'< / a>');

在上面的代码中,我使用NUL和SOH字符(ASCII 0x00和0x01) code>< a> 标签,因为它们很可能不会出现在字符串中。随意将它们替换为不会出现在您的字符串中的任何其他字符或字符序列。



从其他评论中可以看出您正在浏览器中运行。在这种情况下,浏览器已经将HTML解析为一个很好的DOM树。使用DOM方法来解析树并按照你想要的方式处理它:

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

//过滤掉不需要的节点以加快处理速度。
//例如,您可以忽略'SCRIPT'节点等
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;
}
//序列化整个文档:
var simpleDocument = simpleHTML(document.body);

//序列化一个div:
var simpleDiv = simpleHTML(document.getElementById('some_div'));

//过滤一个html格式化的字符串:
var temp = document.createElement('DIV');
temp.innerHTML = original_string;
simple_string = simpleHTML(temp);


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

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