Hacker Newsnew | past | comments | ask | show | jobs | submitlogin
A Google Javascript Mystery
5 points by emmett on Sept 30, 2008 | hide | past | favorite | 8 comments
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:

    var ja = /function (^\w+)/;
    function ia(a){
      var b = ja.exec(String(a));
      if (b) return b[1];
      return "";
    }
As far as I can tell, that regular expression does not match any 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.


In (^\w+) the ^ is not a negation of the expression as it would be in [^\w+]. It simply denotes 'begins with.'


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


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.


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.


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?


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?


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.


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




Guidelines | FAQ | Lists | API | Security | Legal | Apply to YC | Contact

Search: