4 ms·
Ask HN: Regular Expression to Remove Script Tags and Their Contents?
Hello,
I am currently working on a Ruby web parser for reading webpages and extracting the text only. I am using the following lines of code to strip scripts, styling, and tags:
cleaned = html.gsub(/<[^(>|\/)]*?script.*?>[^<]*?<.*?\/script.*?>/, "")
cleaned = cleaned.gsub(/<[^(>|\/)]*?style.*?>[^<]*?<.*?\/style.*?>/, "")
cleaned = cleaned.gsub(/<\/?[^>]*>/, "")
The only problem is that if the script tag contains a less than sign such as:
<script type='text/javascript'>
var foo=3;
if(foo<5) {
alert("bar")
}
</script>
In that case the first regular expression for removing script tags and their contents does not work. It all boils down to the part of the regular expression:
[^<]*?
I can't seem to figure out a better way though to make the star grab everything in a lazy fashion (in other words don't match the first opening script tag with the last opening tag in the HTML).
Has anyone else worked around this problem in the past? Are there any regular expression ninjas who might be able to lend a hand?
I thank you in advance for your time and consideration.
- gaius 16y ago"ninjas" <- http://news.ycombinator.com/item?id=1591573 http://news.ycombinator.com/item?id=1591573 Anyway, try the Ruby port of Python's BeautifulSoup: http://www.crummy.com/software/RubyfulSoup/ http://www.crummy.com/software/RubyfulSoup/
- NathanKP 16y agoninjas I was thinking of the XKCD comic where the hero swings in to save the day with his regular expression. ;) I will look at Beautiful Soup and see if that can work as an alternate technique. Thank you.
- byoung2 16y agohttp://wonko.com/post/sanitize http://wonko.com/post/sanitize Sanitize, a whitelist-based HTML sanitizer written in Ruby. Given a list of acceptable elements and attributes, Sanitize will remove all unacceptable HTML from a string.
- lhorie 16y agoYou'll probably want to read this: http://stackoverflow.com/questions/1732348/regex-match-open-tags-except-xhtml-self-contained-tags http://stackoverflow.com/questions/1732348/regex-match-open-...
- NathanKP 16y agoOkay, I get the point. Trying to write regular expressions instead of using a real XML parser is a bad idea.