Why does the JavaScript RegExp /^\w+$/ match undefined?

firefox, javascript, regex

Solution

When `undefined` is cast to a string (which the regex does), it produces the string `"undefined"`, which is then matched.

Problem

Why does the the RegExp `/^\w+$/` match `undefined`? Example code: ``` alert(/^\w+$/.test(undefined)); ``` This will display true in Firefox 3 (only browser I tested it on).

Original source