Hacker Newsnew | past | comments | ask | show | jobs | submitlogin

    "If the first argument to String.replace() is a string,   
    only the first instance of that string is replaced. To
    replace all instances, a regex is necessary"
That's not that much advanced javascript knowledge.


I disagree. I think fault lies with the language designer(s) for putting the equivalent of a pull-handle on a push-door. The only way to reliably avoid this error would be to spend an inordinate amount of time reading documentation, dutifully examining every aspect of basic functionality for unusual behavior. Every other string replace method I've encountered does global replace. Java's String.replace(), STL's std::replace, PHP's str_replace, Python's string.replace(), all of them replace every instance. Given this precedence, it's entirely reasonable to assume JavaScript works the same. Unfortunately, it doesn't.

I've been writing JavaScript off-and-on since I was 12. I've used Node.js extensively enough to find bugs in it, including one that ended up being a bug in V8[1][2]. If I'm not an experienced JavaScript developer, then very few are.

1. http://geoff.greer.fm/2012/05/30/leaky-abstractions-a-journe...

2. https://github.com/joyent/node/issues/3167


It's not necessarily "advanced", but it is unexpected behavior I think. I've run into this same sort of issue myself. It didn't take ages to figure out, but it was annoying. It's one of those things that separates an experienced JavaScript dev from the rest.


    "You could stare at that function for hours without seeing 
    the problem. It requires knowledge of how JavaScript’s
    String.replace() works:"
Okay, but "knowledge of how JavaScript's String.replace() works" is showing that the experience of those guys is not the greatest. I'm pretty sure every decent JS developer knows this after a short amount of time, because "String.replace" is used pretty often.




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

Search: