LoginSignup
4
4

More than 5 years have passed since last update.

String.prototype.match の返り値が IE8 だけ異なる

Last updated at Posted at 2012-12-21

JavaScript の String.prototype.match で、マッチしなかった () の値が IE だけ異なる。

// foo または数字にマッチし、数字だったら後方参照したい
pattern = /foo|(\d+)/;

// () にマッチする場合は期待通り
number = "123";
number.match(pattern); // ["123", "123"]

// () にマッチしない場合は IE だけ異なる
text = "foo";
text.match(pattern);
// ["foo", undefined] (Chrome 23, Safari 6, Firefox 17, Opera 12, IE9)
// ["foo", ""] (IE8)

IE10 は環境が手元にないので未検証。
ECMAScript 3 / 5 では undefined になるのが正しい挙動 (コメント参照) で、IE8 の挙動がおかしい。

4
4
3

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
4
4