So, to drive it home, this is not good:
new RegExp("[\\n\\r]+", "gim")
And should be replaced with:
new RegExp("[\\n\\r]+", "gi")
Want to use code from this post? Check out the license.