undefined cast to a string with regex test

gaz Heyes gazheyes at gmail.com
Wed Oct 10 03:19:05 PDT 2012


This wasn't totally unexpected but seriously made me go WTF. It appears
that arguments to regexp.test are cast to a string. This was bad for me
since I was writing a js parser at the time and checking the argument to
see if it matched one of the reserved words. See the example below:-

if(/^(?:undefined|ok|wtf|js)$/.test(undefined)){
  alert('WTF');
} else {
   alert('NO WTF');
}

That's pretty much all I got but if you didn't go WTF then I apologise for
the wasted thread.
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.mozilla.org/pipermail/es-discuss/attachments/20121010/f6eb4ed0/attachment.html>


More information about the es-discuss mailing list