Created
January 12, 2012 10:38
-
-
Save tlxue/1599791 to your computer and use it in GitHub Desktop.
HTML Format
This file contains hidden or 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
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