Skip to content

Instantly share code, notes, and snippets.

@tlxue
Created January 12, 2012 10:38
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save tlxue/1599791 to your computer and use it in GitHub Desktop.
Save tlxue/1599791 to your computer and use it in GitHub Desktop.
HTML Format
function HTML_format(original_HTML){
var o = original_HTML,
match;
o = o.replace(/<div><\/div>/g,'')
// todo: replace div block inside div block, now we only deal with one-layer nesting
// method: a,b find bs see if the one before is a, if it is. eliminate them both
o = o.replace(/^(<div>)+/g,'');
o = o.replace(/(<\/div>)+$/g,'');
match = /(<\/?div>)+<(ol|ul|sup|img|blockquote)>|<\/(ol|ul|sup|img|blockquote)>(<\/?div>)+/g.exec(o);
while(match){
if(match[1]){
o = o.replace(match[0], '<' + match[2] + '>');
}else{
o = o.replace(match[0], '</' + match[3] + '>');
}
match = /(<\/?div>)+<(ol|ul|sup)>|<\/(ol|ul|sup)>(<\/?div>)+/g.exec(o);
}
// http://stackoverflow.com/questions/4724701/regexp-exec-returns-null-sporadically
o = o.replace(/(<br>)?(<\/?div>)+/g,'<br>');
// get things done
o = o.replace(/<\/sup><sup>/g,'<br>');
return o;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment