admin管理员组文章数量:1278820
I´m using the following REGEXP:
$output = preg_replace( "/\/\/(.*)\\n/", "", $output );
The code works well BUT!!!!, when a URL like (), the code replaces it... (http://)
What can you do to no replace that URLs.
Thanks,
I´m using the following REGEXP:
$output = preg_replace( "/\/\/(.*)\\n/", "", $output );
The code works well BUT!!!!, when a URL like (http://this_is_not_a_ment./kickme), the code replaces it... (http://)
What can you do to no replace that URLs.
Thanks,
Share Improve this question asked Nov 25, 2010 at 15:44 CRISHK CorporationCRISHK Corporation 3,0086 gold badges39 silver badges53 bronze badges 2- 2 You need some kind of parser that can distinguish between code and ments. – Gumbo Commented Nov 25, 2010 at 15:46
- 2 You should look at this answer: stackoverflow./questions/1732348/… – Don Roby Commented Nov 25, 2010 at 15:53
2 Answers
Reset to default 8You need a regular expression that can distinguish between the code and the ments. In particular, since the sequence of //
can either be in a string or a ment, you just need to distinguish between strings and ments.
Here’s an example that might do this:
/(?:([^\/"']+|\/\*(?:[^*]|\*+[^*\/])*\*+\/|"(?:[^"\\]|\\.)*"|'(?:[^'\\]|\\.)*')|\/\/.*)/
Using this in a replace function while replacing the matched string with the match of the first subpattern should then be able to remove the //
style ments.
Some explanation:
[^/"']+
matches any character that is not the begin of a ment (both//…
and/*…*/
) or of a string/\*(?:[^*]|\*+[^*/])*\*+/
matches the/* … */
style ments"(?:[^"\\]|\\.)*"
matches a string in double quotes'(?:[^'\\]|\\.)*'
matches a string in single quotes\/\/.*
finally matches the//…
style ments.
As the first three constructs are grouped in a capturing group, the matched string is available and nothing is changed when replacing the matched string with the match of the first subpattern. Only if a //…
style ment is matched the match of the first subpattern is empty and thus it’s replaced by an empty string.
But note that this may fail. I’m not quite sure if it works for any input.
$output = preg_replace( "/(?<!\:)\/\/(.*)\\n/", "", $output );
本文标签: phpRegular expression for clean javascript comments of type Stack Overflow
版权声明:本文标题:php - Regular expression for clean javascript comments of type- Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1741277900a2369837.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论