I need to remove all comments from a stript. Comments always start from '//'. My RegEx /\/\/.*/g
does it but it removes substring if it inside quotes and I want avoid that behavior. Is there an ability to use RegEx for removing comments which are located only outside of quotes?
Asked
Active
Viewed 45 times
-1

Michel_T.
- 2,741
- 5
- 21
- 31
-
So you are saying that it should **not** remove something like `"foo //bar"`? – Kaspar Lee Oct 18 '16 at 14:26
-
I remember this homework from school. :) – Luis Lavieri Oct 18 '16 at 14:26
-
Yes, if there is something like: str="foo//bar//string" it shouldn't remove it, only if slashes are either before or after string. – Michel_T. Oct 18 '16 at 14:30
-
[This question may help.](http://stackoverflow.com/q/4568410) – Kaspar Lee Oct 18 '16 at 14:31
-
Nope, unfortunately, it didn't help. – Michel_T. Oct 18 '16 at 15:28
1 Answers
0
You can use negative lookahead (but I don't think this will match //foo "string"
)
(?!.*")\/\/.*

rusheb
- 1,004
- 9
- 15