My understanding is that one can use either of the two methods for case-insensitive regex based matches in JavaScript: match(/pattern/i) or match("pattern","i"). I’m not getting the second variation to work in Chrome though. (I’m using Chromium 14.0.835.202). Is this a bug in Chrome? (or a user error?)
In Firefox when I run this code I get: Hello World and then Hello World. In Chrome I get Hello World, undefined.
<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>
No, it’s not a bug. Firefox allows the use of the
flagsparameter inString.match, but as noted on the Mozilla Documentation (or rather, as it used to be noted – this functionality is no longer even mentioned) it’s nonstandard and should be avoided. Also, it’s usually less efficient.If you need this functionality, use
new RegExpinstead.