Skip to content

Instantly share code, notes, and snippets.

@rettal
Forked from Idered/script.js
Last active January 2, 2016 09:49
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
Star You must be signed in to star a gist
Save rettal/8285451 to your computer and use it in GitHub Desktop.
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