getButterfly
Resume

NAVIGATION

ATTRIBUTION

getButterfly
Dublin, Ireland

All Content Copyright ©2019
getButterfly

Useful JavaScript String Prototypes

This is a function to remove all double whitespaces and preceding line breaks.

/**
 * Prototype function to remove all double whitespaces and preceding line breaks
 */
String.prototype.allTrim = String.prototype.allTrim || function() {
    return this
        .replace(/ +/g, ' ')
        .replace(/\n\s*\n/g, '\n')
        .replace(/(\r\n|\n|\r)/gm, '') // Remove all 3 types of line breaks
        .replace(/^,+/, '')
        .replace(', ,', ',')
        .trim();
};

// Usage:
// string.allTrim();

This is a function to replace all line breaks with commas.

/**
 * Prototype function to replace all line breaks with commas
 */
String.prototype.allCommas = String.prototype.allCommas || function() {
    return this
        .replace(/(\r\n|\n|\r)/gm, ', ')
        .allTrim()
        .trim();
};

// Usage:
// string.allCommas();

Enjoy!

Find more JavaScript tutorials, code snippets and samples here or more jQuery tutorials, code snippets and samples here.

Do you want better SEO? More traffic? More conversions? More growth? We help companies exponentially grow their traffic and conversions, while outranking their competitors. With more than 10 years of experience, we’ve learned what is valuable to our clients.
SEO Dublin | SEO Malta

My Battle Tested Recommendations

Jetpack
Dreamhost
WordPress.com
CodeCanyon
SEMrush

Disclaimer: These recommendations contain affiliate links.


Privacy Policy