Internet Explorer 8 JScript正则表达式错误 [英] Internet explorer 8 JScript regular expression bug

查看:152
本文介绍了Internet Explorer 8 JScript正则表达式错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用Windows XP上的Internet Explorer 8进行测试,并且遇到了一个繁琐的错误。我试图用一个在firefox中运行正常的正则表达式测试字符串,并在ie8控制台中独立进行精细测试。

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

更详细的代码:不像早期的代码片段那样漂亮或干净。

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;

    }

对于 mergeArrays 功能 pr_left & pr_right 是TR对象的jQuery列表。并且我将行的 pr_rowIndex-th 单元格中的文本比较为两行。

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 isThan。

dateRegEx.test (a)返回false,因为字符串a被窃听。

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

在ie8控制台中测试它告诉我

Testing in the ie8 console it tells me that

globalA.toLowerCase() == "sep 2010"

返回false。在这种情况下, alert(globalA)显示sep 2010, alert(globalLeft)显示Sep 2010 。我已将两者的.length检查为预期的8。

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 似乎是真的但两者都不等于普通字符串。

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

我完全看不出为什么JScript无法识别这两个字符串。在控制台中重现这些确切的步骤可以按预期工作。

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

[进一步编辑]

转出不同的实现javascript将非空格字符视为空格字符类或非空格字符类。无论这是一个错误还是按预期工作,都可以讨论。

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.

推荐答案

如上所述。这里的问题是,javascript中的\ s不包括IE中的非中断空格,但在FF中包含非中断空格。

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.

这篇关于Internet Explorer 8 JScript正则表达式错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆