Skip to content

Instantly share code, notes, and snippets.

@captDaylight
Created February 21, 2018 16:54
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save captDaylight/bbc44c602de302b986de018b807a2219 to your computer and use it in GitHub Desktop.
Save captDaylight/bbc44c602de302b986de018b807a2219 to your computer and use it in GitHub Desktop.
Get the headers from markdown along with the header type
var re = /(#+)(.*)/;
var str = '## About Bottler\n\nThis is something about the project\n\n# something else\n';
function findAll(regex, sourceString, aggregator = []) {
const arr = re.exec(sourceString);
if (arr === null) return aggregator;
const newString = sourceString.slice(arr.index + arr[0].length);
return findAll(regex, newString, aggregator.concat([arr]));
}
findAll(re, str);
// [ ["## About Bottler", "##", " About Bottler"], ["# something else", "#", " something else"] ]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment