Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
439 views
in Technique[技术] by (71.8m points)

javascript - Internet explorer 8 JScript regular expression bug

I'm testing in internet explorer 8 on windows XP and hitting into a tedious bug. I am trying to test strings with a regular expression that works fine in firefox and fine tested independently in the ie8 console.

But when it through my closure function the string acts strangely

[Edit] More detailed code: Not as nice or clean as the earlier snippet.

var m_TableSorter = (function() {

    // static reg expression string and array for ordering dates
    var dateRegEx = new RegExp(
     "(jan|feb|mar|apr|may|jun|jul|aug|sep|oct|nov|dec)\s\d{4}");
     ...
     ...
    function greaterThan(left, right) {
        window["globalLeft"] = left;
        window["globalRight"] = right;
        var a = $.trim(left.toLowerCase());
        var b = $.trim(right.toLowerCase());
        window["globalA"] = a.toString();
        window["globalReg"] = dateRegEx;
        if (dateRegEx.test(a) && dateRegEx.test(b)) {
            var yearA = parseInt(a.substring(4,8), 10);
            var yearB = parseInt(b.substring(4,8), 10);
            if (yearA > yearB) {
                return true;
            } else if (yearB > yearA) {
                return false;
            } else {
                /* ... */
                var monthA =
                    $.inArray(a.substring(0,3).toUpperCase(), monthArray);
                var monthB = 
                    $.inArray(b.substring(0,3).toUpperCase(), monthArray);
                m_Debug.tryAssert(monthA >= 0, "Date string malformed");
                m_Debug.tryAssert(monthB >= 0, "Date string malformed");
                if (monthA > monthB) {
                    return true;
                } else {
                    return false;
                }
            }
        }
        //alert("NONDATE");
        if ( a.toUpperCase() >= b.toUpperCase() ) {
            return true;
        }
        return false;
    }

    function mergeArrays(pr_left, pr_right, pr_compareFunction, pr_rowIndex) 
    {
        m_Debug.debugUtilityFunction(arguments);
        var results = new Array();
        var obj;
        /* Merges in order so that if right > left then the results is
         * [left right] otherwise the result is [right left] (Dependant on
         * ascending or descending order)
         */
        while (pr_left.length > 0 || pr_right.length > 0) {
            if (pr_left.length > 0 && pr_right.length > 0) {
                window["globalLeft1"] = $(pr_left[0].children[pr_rowIndex]).text().toString();
                window["globalRight1"] = $(pr_right[0].children[pr_rowIndex]).text().toString();
                var bool = pr_compareFunction(
                    $.trim($(pr_left[0].children[pr_rowIndex]).text()),
                    $.trim($(pr_right[0].children[pr_rowIndex]).text())
                );
                if (bool) {
                    results.push(pr_left.shift());
                } else {
                    results.push(pr_right.shift());
                }
            } else  if (pr_left.length > 0) {
                for (obj in pr_left) {
                    results.push(pr_left[obj]);
                }
                break;
            } else if (pr_right.length > 0) {
                for (obj in pr_right) {
                    results.push(pr_right[obj]);
                }
                break;
            }
        }
        return results;

    }

For the mergeArrays function pr_left & pr_right are jQuery list of TR objects. and im comparing the text in the pr_rowIndex-th cell of the row for two rows.

pr_compareFunction is greaterThan.

dateRegEx.test(a) returns false because the string a is bugged.

Testing in the ie8 console it tells me that

globalA.toLowerCase() == "sep 2010"

returns false. In this case alert(globalA) has shown "sep 2010" and alert(globalLeft) has shown "Sep 2010". I've checked .length on both to be 8 as expected.

globalLeft1 == globalLeft seems to be true but neither are equal to normal strings.

I can't see at all why JScript cannot recognise these two strings. reproducing these exact steps in the console works as expected.

[Further Edit]

Turn's out different implementation of javascript treat non breaking space characters as either a space character class or non-space character class. Whether this is a bug or working as intended is open to discussion.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

As mentioned. The issue here was that "s" in javascript does not include a non breaking space in IE but includes a non breaking space in FF.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...