Port @daveschoutens's RegExp fixes to the 0.4.x branch. Resolves #39. /cc @sapegin