match “//” comments with regex but not inside a quote

后端 未结 4 553
佛祖请我去吃肉
佛祖请我去吃肉 2020-12-20 01:33

I need to match and replace some comments. for example:

$test = \"the url is http://www.google.com\";// comment \"<-- that quote needs to be matched
         


        
4条回答
  •  [愿得一人]
    2020-12-20 01:53

    Don't forget that PHP comments can also take the form of /* this is a comment */ which can be span across multiple lines.

    This site may be of interest to you:

    http://blog.stevenlevithan.com/archives/mimic-lookbehind-javascript

    Javascript does not have native lookbehind support in it's regular expression engine. What you may be able to do is start at the end of a line and look backward to capture any characters that follow a semi colon + optional whitespace + // So something like:

    ;\w*\/\/(.+)$
    

    This may not capture everything.

    You also may want to look for a Javascript (or other languages) PHP syntax checker. I think Komodo Edit's PHP syntax checker may be written in Javascript. If so, it may give you insight on how to strip everything out but comments as the syntax checkers need to ensure the PHP code is valid, comments and all. The same can be said about syntax color changers. Here are two other links:

    http://ecoder.quintalinda.com/

    http://www.webdesignbooth.com/9-useful-javascript-syntax-highlighting-scripts/

提交回复
热议问题