Home & blog  /  2011  /  Mar  /  view post  /

Bizarre webkit bug with 'typeof'

posted: 01 Mar '11 08:28 tags: Javascript, Webkit, browser, bug, REGEXP

That'll teach me to post scripts and assume, without checking, that the normally-reliable Chrome and Safari will play ball. The other day I posted my JS simulation of REGEXP lookbehinds, but Marcus Tucker found it didn't work in these browsers.

After some digging, it seems there's a bizarre bug in Webkit browsers (i.e. Chrome and Safari) whereby regular expression objects are considered functions, not objects. So:

alert(typeof /\w+/);

Opera, IE and FF all say 'object', as you'd expect, but Chrome and Safari say 'function'. Very odd. I fixed the issue by using instanceof rather than typeof:

1alert(/\w+/ instanceof RegExp)

2//all browsers say true - happy days!

post new comment

Comments (0)