regular expression gives different output in FF and IE

雨燕双飞 提交于 2019-12-04 04:54:07

问题


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.



来源:https://stackoverflow.com/questions/6440436/regular-expression-gives-different-output-in-ff-and-ie

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!