【问题标题】:Does Chrome handle match correctly?Chrome 处理是否正确匹配?
【发布时间】:2011-11-13 04:27:41
【问题描述】:

我的理解是,可以在 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>

【问题讨论】:

    标签: javascript regex match


    【解决方案1】:

    不,这不是错误。 Firefox 允许在 String.match 中使用 flags 参数,但正如 Mozilla 文档中所述(或者更确切地说,正如它使用要注意的那样 - this functionality is no longer even mentioned)它是非标准的,应该避免.此外,它通常效率较低。

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

    【讨论】:

    • 而且,ECMAscript v5.1 Spec 的第 145 页没有提到 .match() 的第二个参数
    • 谢谢。正是我需要的。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2023-03-27
    • 1970-01-01
    • 2011-10-14
    • 2013-10-26
    • 1970-01-01
    • 1970-01-01
    • 2013-03-08
    相关资源
    最近更新 更多