Remove Multiple New Lines with JavaScript
I'm blessed in that lots of people want to guest post on this blog. It's really flattering and I love seeing writers get a bunch of attention after writing. My task is converting the blog post, in whatever format it's provided in (HTML, Markdown, PDF, Google Doc, etc.), to HTML for my blog, which can sometimes get messy. I employ a host of regular expressions to fix these formatting issues. And the number one problem? Loads of extra new lines (\n
).
The Regular Expression
The regular expression is actually quite simple:
content.replace(/[\r\n]+/g, '\n'); // Just one new line
content.replace(/[\r\n]+/g, '\n\n'); // "document" formatting, more elegant
With the dozens of extra lines gone it's much easier to work with the content!
MooTools has always gotten a bit of grief for not inherently using and standardizing namespaced-based JavaScript classes like the Dojo Toolkit does. Many developers create their classes as globals which is generally frowned up. I mostly disagree with that stance, but each to their own. In any event...
One of the worst kept secrets about AJAX on the web is that the underlying API for it, XMLHttpRequest
, wasn't really made for what we've been using it for. We've done well to create elegant APIs around XHR but we know we can do better. Our effort to...
One of my favorite uses of the MooTools JavaScript library is the SmoothScroll plugin. I use it on my website, my employer's website, and on many customer websites. The best part about the plugin is that it's so easy to implement.
I recently ran...
dwProgressBar was a huge hit when it debuted. For those of you who didn't catch my first post, dwProgressBar is a MooTools 1.2-based progress bar which allows for as much flexibility as possible. Every piece of dwProgressBar can be controlled by CSS...
David,
I’m more inclined to use something like:
Only there always seems to be some extra whitespace between those newlines. If you don’t want to loose the tabs on the next line then this works just as well