Chrome处理是否正确匹配?

sno*_*guy 1 javascript regex match

我的理解是,可以在JavaScript中使用两种方法中的任何一种用于不区分大小写的基于正则表达式的匹配:match(/pattern/i)match("pattern","i").我没有让第二种变体在Chrome中运行.(我正在使用Chromium 14.0.835.202).这是Chrome中的错误吗?(或用户错误?)

在Firefox中,当我运行此代码时,我得到:Hello World,然后是Hello World.在Chrome中,我得到了Hello World,未定义.

<html>
<head>
</head>
<body>

<input type="button" id="button" value="Click me!" onclick="buttonClick()" />

</body>

<script language="javascript">
function buttonClick()
{
  str="Hello World"
  alert(str.match(/hello world/i))
  alert(str.match("hello world","i"))
}
</script>
</html>
Run Code Online (Sandbox Code Playgroud)

Ry-*_*Ry- 6

不,这不是一个错误.Firefox允许使用flags参数String.match,但正如Mozilla文档中所述(或者更确切地说,正如之前指出的那样 - 甚至不再提及此功能)它是非标准的,应该避免使用.而且,它通常效率较低.

如果您需要此功能,请new RegExp改用.

  • 并且,[ECMAscript v5.1 Spec](http://www.ecma-international.org/publications/files/ECMA-ST/Ecma-262.pdf)的第145页没有提到`.match的第二个参数. ()` (2认同)