gpt4 book ai didi

IE7 中的 javascript 拆分正则表达式错误

转载 作者:塔克拉玛干 更新时间:2023-11-02 20:28:43 24 4
gpt4 key购买 nike

我试图在 javascript 中使用此正则表达式 ({[^{}]*}) 进行拆分,但我在 IE7 和 FF 中得到了不同的结果。 firefox 结果是正确的。

<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 库?

最佳答案

参见 this old blog post.split() 正则表达式中处理捕获组的变化的可能解决方案。

来自那篇文章:

  • Internet Explorer excludes almost all empty values from the resulting array (e.g., when two delimiters appear next to each other in the data, or when a delimiter appears at the start or end of the data). This doesn't make any sense to me, since IE does include empty values when using a string as the delimiter.
  • Internet Explorer and Safari do not splice the values of capturing parentheses into the returned array (this functionality can be useful with simple parsers, etc.)
  • Firefox does not splice undefined values into the returned array as the result of non-participating capturing groups.
  • Internet Explorer, Firefox, and Safari have various additional edge-case bugs where they do not follow the split specification (which is actually quite complex).

利维坦的 XRegExp库确实很小而且很有用,并且包含修复程序。

关于IE7 中的 javascript 拆分正则表达式错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4417931/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com