Javascript: The Good Parts and string.split problem

  • Written by: Marko Samastur
  • Published on:
  • Category: Javascript, Web

I just read Douglas Crockford ‘s book JavaScript: The Good Parts . It’s what a technical book should be. Concise yet full of useful information as a result of clear scope and sticking to it. If my experience is anything to go by, then there are not many Javascript programmers who wouldn’t benefit from it.

I did found one bit that wasn’t quite right. On pages 91-92 Douglas discusses string.split method, which can take a regular expression as a separator with which to split string. Douglas rightly points out that if regular expression includes capturing group (stuff between paranthesis like b(mmm)c ), then strings matching those groups will be included in the split.

However Douglas is not completely correct when he says that some implementations drop empty strings in the output array when separator is regular expression. If you run this demo in Firefox3 and IE7, you’ll see that IE7 drops even non-empty strings from capturing groups, while Firefox3 includes them.

Still, I would find a bug in my code faster this week if I read his book sooner.