3

8 以降のすべての IE バージョンで同じように機能する「分割」機能が必要です (はい、これはWebBrowserコントロールを使用するアプリケーション用であるため、IE のみです)。次のコードは、IE 8 では「2」、IE 9+ では「3」を警告します。

var step1_slices = "One.Two.".split(new RegExp("\\.", "g"));
alert(step1_slices.length);

私はこの問題に遭遇した最初の人ですか、それとも既知の解決策がありますか?

4

1 に答える 1

2

Am I the first one to run across this problem

No, older browsers implemented a split differently when the delimeter was at the start or end of a string. Some kept the empty strings at either end, some kept the final one, some, like IE, only kept empty matches if they are in the body of the string. The modern browsers all keep both outriders.

The simplest way to account for any difference is to make IE9+ act like IE8- look at the first and last elements of the array, and remove them if they are undefined or the empty string.

String.prototype.split8=function(delim){
  var A=this.split(delim);
  if(!A[0])A.shift();
  if(!A[A.length-1)A.pop();
  return A;
}
于 2013-04-16T02:28:47.370 に答える