

A Google Javascript Mystery - emmett

I've been doing some investigation in how ad managers (Google AdManager, OpenX) work, and in looking at the Google JS I came across a very strange snippet of code:<p><pre><code>    var ja = /function (^\w+)/;
    function ia(a){
      var b = ja.exec(String(a));
      if (b) return b[1];
      return "";
    }
</code></pre>
As far as I can tell, that regular expression does not match <i>any</i> string. What's going on? Is there a bug in the google compressor? Is this some kind of red herring? From usage, it looks like it's supposed to extract function names, but that regular expression doesn't actually work.
======
qhoxie
In (^\w+) the ^ is not a negation of the expression as it would be in [^\w+].
It simply denotes 'begins with.'

~~~
noodle
to add a bit more specificity to this answer, the ^ matches the start of a
string or line.

~~~
emmett
Try it; it doesn't seem to actually do this. The regex isn't in multi-line
mode anyway, and the ^ is not at the front of the regex.

~~~
noodle
i wasn't commenting on the functionality of the code snippet, but the
functionality of ^ in regexes.

edited to add this:

so i went ahead and actually looked at the snippet. it looks like it might be
a helper function in call stack debugging.

~~~
emmett
I agree. That's what it seems to be for. What I don't get is...how does that
regex work? Can anyone give me a sample string that actually matches it?

~~~
noodle
i don't know for sure. i tried testing it out on closures and other random
things and it doesn't seem to work on anything i can think of. perhaps its a
typo?

------
ionfish
If you remove the caret, it matches function names; with it, it won't work
since there's no way to match the beginning of the string at that point.

------
trickjarrett
It looks to me like it matches every string composed of only letters and
numbers.

