I find the String.replace() functionality of JavaScript to be a bit more than annoying. This is because of one crucial reason.
When you do this:
'Hey there!'.replace('e', 'a');
coming from most backgrounds, you’d expect this:
'Hay thara!'
but you end up with this:
'Hay there!'
Wha?? Talk about running into debug issues and pulling your hair out.
The issue is that the default behavior is to just replace the first instance of a string. Personally, I’m not quite sure why this is the case, but the fact remains that it is. Now, another option is to use regular expressions. Let’s try:
'Hey there!'.replace(/e/, 'a');
Still, no dice. You get:
'Hay there!'
Egads!
However, if you do:
'Hey there!'.replace(/e/gi, 'a');
You’re then using the global regular expression replacement, so you get:
'Hay thara!'
Finally, just what we were looking for! So, note that anytime you do 'original string to search within'.replace(/string to look for/gi, 'string to replace with'), then you’ll end up with what you expected – a global replace-all function throughout the string.
January 19, 2012
I recently read a misleading and misinformed Time article about the space elevator that got me a little steamed.
One thing in particular was the dismissive tone. It was never really considered from an engineering standpoint of an engineer. Certainly it sounds like a pie-in-the-sky idea at first, but when someone does some research they find it’s not as far-fetched as they originally thought. The idea that is costs “kazillion zillions” is entirely fictitious. At least do some research and use real numbers.
The real pricetag is between $8 billion and $20 billion, or .2-.5% of the 2012 US federal expenditure, which you can compare to NASA’s 2012 budget of $19 billion.
Now, things can certainly go overbudget, it happens all the time, but even if it still costs ten times as much, the benefits outweigh the problems significantly.
In a nutshell, any writer should be far more educated on the topic and do some actual fact-checking before writing a terribly misinformed article.
And I fully understand if I sound like a space elevator fanboy, I love the idea and have read a lot of the literature on it, it’s something I get really excited abuot – I’m not quite sure how one wouldn’t.
December 5, 2011
I recently was talking to some friends about the current state of browsers and operating systems and how the next year or two could really shake up what’s been traditionally considered the norm. You know, the IE sucks, Chrome is the fastest and most standard-compliant browser, OS X has the best usability and Windows is for the plebes mode of thinking that people have fallen into. It’s an easy mode to get used to, but that could be changing.
First, here’s some benchmarks from September 27th from Lifehacker. There’s an article about a very promising IE10, then there was a follow-up two days later about the upcoming Opera build.
As for Windows 8, here’s a good video from June demoing some features and the updated UI (the Internet’s Wadsworth Constant applies, so feel free to skip to 30% in).
Granted, until you use it yourself, it’s hard to say what the truth is when updates are announced. Personally, I thought the iPad UI seemed silly because it was just a spread out iPod/iPhone UI with wasted space, but using it is still really nice. So, we’ll see how things change.
November 21, 2011