Skip to content

Instantly share code, notes, and snippets.

@ultim8k
Created July 9, 2014 15:34
Show Gist options
  • Save ultim8k/1ff3513237c27a192148 to your computer and use it in GitHub Desktop.
Save ultim8k/1ff3513237c27a192148 to your computer and use it in GitHub Desktop.
Regular Expressions for manipulating linebreaks (needs review)
var test_str = 'test\n\n\nha\n\n\n\n\n\n\n\nbla\nbla\n';
// Replace more than two \n with two \n
var data = test_str.replace(new RegExp('(\n){3,}', 'gim') , '\n\n');
// normalize line endings to \n, because \r is not recognized as "end of line"
data = data.replace (/\s*\R/g, "\n");
// remove leading and trailing whitespace
data = data.replace (/^\s*|[\t ]+$/gm, "");
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment