Strange JavaScript Regular Expression Behavior
- by Kiwi
I'm getting different behavior from a regular expression in JavaScript depending on whether or not I declare it using literal syntax. Using a extremely simple test HTML file:
<html>
<head>
<script type="text/javascript">
var s = '3';
var regex1 = /\d/;
var regex2 = new RegExp('\d');
alert(s.search(regex1)); // 0 (matches)
alert(s.search(regex2)); // -1 (does not match)
</script>
</head>
<body></body>
</html>
The regular expression declared with literal syntax (/\d/) works correctly, while the other (new RegExp('\d')) does not. Why on earth is this happening?
I'm using Google Chrome 5.0.375.70 on Windows Vista Home Premium, if that's at all helpful.