Created
January 5, 2014 04:41
-
-
Save Idered/8264412 to your computer and use it in GitHub Desktop.
Remove first and last empty lines, remove useless indentation. Good for parsing HTML
This file contains 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 str = '\n\n\n Multiline text\n with indentation\n damn.\n\n\n'; | |
// remove first and last empty lines | |
str = str.replace(/^[\r\n]+|[\n\r]+$/gi, ''); | |
var indentTab = str.match(/\t*/) != '', | |
indentSize = indentTab ? 1 : str.match(/\s*/)[0].length, | |
regex = new RegExp('^(?:' + (indentTab ? '\\t' : ' {' + indentSize + '}') + ')', 'mg'); | |
// Remove indent | |
if (indentSize) | |
str = str.replace(regex, ''); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment