首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >php函数preg_replace正则表达式不工作,出现语法问题

php函数preg_replace正则表达式不工作,出现语法问题
EN

Stack Overflow用户
提问于 2010-12-14 02:39:55
回答 1查看 196关注 0票数 0

我正在尝试在受控脚本情况下使用preg-replace删除不必要的注释,但我的regex是不正确的。有人知道我的正则表达式有什么问题吗?(我有Apache/2.0.54和PHP/5.2.9

之前:

代码语言:javascript
复制
// Bla Bli Blue Blow Bell Billy Bow Bye
script var etc ();    // cangaroo cognac codified cilly celine cocktail couplet
script http://blaa.org    // you get the idea!

之后:

代码语言:javascript
复制
script var etc ();
script http://blaa.org

问题:使用什么正则表达式?

代码语言:javascript
复制
# when comment starts on a new line, delete this entire line
# find [a new line] [//] [space or no space] [comment]
$buffer = preg_replace('??', '??', $buffer);

# when comment is halfway in script (    //  comment)
# find [not beginning of a line] [1 TAB] [//] [1 space again] [comment]
$buffer = preg_replace('??', '??', $buffer);

任何和所有的建议都会得到我的重视,因为我离解开这个谜语如此之近!

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2010-12-14 02:45:42

试试这个正则表达式:

代码语言:javascript
复制
/(?<!http:)\/\/[^\r\n]*/

不过要小心,考虑如下字符串:

代码语言:javascript
复制
<!-- 
// not a comment -->

代码语言:javascript
复制
/* 
// not a comment */

代码语言:javascript
复制
var s = "also // not // a // comment";

你可能想要绕过https://...ftp://...等。

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/4432297

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档