Created
July 9, 2014 15:34
-
-
Save ultim8k/1ff3513237c27a192148 to your computer and use it in GitHub Desktop.
Regular Expressions for manipulating linebreaks (needs review)
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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