It may be easier to make out if you had explicit half-columns.
In any case, this works:
import re rx = re.compile(r'.*(//(.*))$') lines = ["// this is a comment", "var x = 2 // and this is a comment too", """var url = "http://www.google.com/" // and "this" too""", """url += 'but // this is not a comment' // however this one is""", """url += 'this "is not a comment' + " and ' neither is this " // only this""",] for line in lines: print rx.match(line).groups()
The conclusion above:
('// this is a comment', ' this is a comment') ('// and this is a comment too', ' and this is a comment too') ('// and "this" too', ' and "this" too') ('// however this one is', ' however this one is') ('// only this', ' only this')
I'm not sure what you are doing with javascript after deleting comments, but JSMin can help. In any case, it removes comments well enough, and exists in python .
Seth
source share