IE7中的javascript拆分正则表达式错误 [英] javascript split regex bug in IE7

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

问题描述

我正在尝试使用javascript中的此正则表达式({[^{}]*})进行拆分,并且在IE7和FF上得到了不同的结果. Firefox的结果是正确的.

I am trying to split with this regex ({[^{}]*}) in javascript and I get different result btw IE7 and FF. The firefox result is the right one.

<style>
.box.round {
    border-radius: 10px;
}
</style>
<script>
jQuery(function ($) {
    $('style').each(function () {
    text = $(this).html();

    alert(text);
    alert(text.split(/({[^{}]*})/));
    // result in FF: .box.round ,{border-radius: 10px;},
    // result in IE7: .box.round
    });
});
</script>

更新 有没有一种方法可以更改正则表达式,使其在IE7中也可以运行而无需添加javascript库?

Update Is there a way to change the regex so it work in IE7 also without adding a javascript library?

推荐答案

我提出了一个适用于regex ({[^{}]*})以及其他可能的解决方案的解决方案.

I made a solution that works on regex({[^{}]*}) and probably others too.

function ieSplit(str, separator) {
    var match = str.match(RegExp(separator, 'g'));
    var notmatch = str.replace(new RegExp(separator, 'g'), '[|]').split('[|]');
    var merge = [];
    for(i in notmatch) {
        merge.push(notmatch[i]);
        if (match != null && match[i] != undefined) {
            merge.push(match[i]);
        }
    }
    return merge;
}
alert(ieSplit(text, '({[^{}]*})'));
// result in FF : .box.round ,{border-radius: 10px;},
// result in IE7: .box.round ,{border-radius: 10px;},

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

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