IE7中的正则表达式兼容性

时间:2013-09-19 16:58:27

标签: regex internet-explorer

尝试使我的JavaScript代码与IE7兼容,并且我遇到了(可能是一个错误)IE7的一些问题。

var elements = url.match(/^:(\d+)(.*)/);

在有效匹配项上返回null:

:80/site1/www
:8080/site2/public_html

不确定如何重写此内容。有什么想法吗?

1 个答案:

答案 0 :(得分:0)

以下是实施差异:

* Internet Explorer ≤ 8 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 behavior differs when using a string as the delimiter.

* Internet Explorer ≤ 8 and older versions of Safari do not splice the values of capturing groups into the returned array.

* In Internet Explorer, captured values for nonparticipating capturing groups are incorrectly returned by RegExp.prototype.exec and String.prototype.match as an empty string rather than undefined, making it impossible to determine group participation.

* Internet Explorer ≤ 8 increments lastIndex after zero-length matches when using RegExp.prototype.exec and RegExp.prototype.test.

* In Internet Explorer, captured values for nonparticipating capturing groups are incorrectly returned by RegExp.prototype.exec and String.prototype.match as an empty string rather than undefined, making it impossible to determine group participation.

使用更简单的RegExp:

var pattern = ":.\\d+/.*";
var data = {"foo":":8080/site2/public_html", "bar":":80/site1/www"};

var elements = (RegExp(pattern).exec(data.foo) + "," + RegExp(pattern).exec(data.bar)).split(",");

使用replaceValue callback替代捕获组以解决浏览器错误。

<强>参考