Regular Expressions with JavaScript http://www.w3schools.com/jsref/jsref_match.asp no \s like in perl, so : use [\s\S]* in regex http://stackoverflow.com/questions/1068280/javascript-regex-multiline-flag-doesnt-work http://www.w3schools.com/jsref/jsref_obj_regexp.asp http://www.regular-expressions.info/javascript.html http://www.javascriptkit.com/javatutors/redev2.shtml https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/RegExp https://developer.mozilla.org/en-US/docs/Web/JavaScript/Guide/Regular_Expressions?redirectlocale=en-US&redirectslug=JavaScript%2FGuide%2FRegular_Expressions http://www.advanced-javascript-tutorial.com/RegularExpressions.cfm#.UmbpgnBwqSo h2. Code Example I assume an easier method exists to do this. h3. Get TOC info code. var txt = "asdfasdf\nasdfasdf\nasdfasdf\n\n\n\nasdfasdf\nasdf"; var res = txt.match(//mg); for (i=0; i