regular expression gives different output in FF an

2019-02-26 09:00发布

问题:

My problem is that when I use this code:

var queuediv = document.getElementById('MSO_ContentTable');
var total = get_text(queuediv);
countTotal = total.split(/\s+/).length;

this is the function

function get_text(el) {
    ret = "";
    var length = el.childNodes.length;
    for (var i = 0; i < length; i++) {
        var node = el.childNodes[i];
        if (node.nodeType != 8) {
            ret += node.nodeType != 1 ? node.nodeValue : get_text(node);

        }
    }
    return ret;
}

it gives me different values in IE and other browser gives same value. So is there a problem with my regexp?

Thanks.

回答1:

You are splitting by white space characters (line breaks, tabs ...). These seems to vary in DOM representation of different browsers. I assume you are trying to split words. Try:

total.split(/ /).length;

or

total.replace(/\n\r\f/, '').split(/\s/).length

you may replace \v and \t also.