我需要匹配并替换一些评论.例如:
$test = "the url is http://www.google.com";// comment "<-- that quote needs to be matched
Run Code Online (Sandbox Code Playgroud)
我希望匹配引号之外的注释,并用"注释替换注释中的任何注释".
我已经尝试了许多模式和不同的运行方式,但没有运气.
正则表达式将使用javascript运行以匹配php"//"注释
更新:我从下面的borkweb拿了正则表达式并修改它.使用了http://ejohn.org/blog/search-and-dont-replace/中的一个函数,得出了这个:
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<title></title>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<script type="text/javascript">
function t_replace(data){
var q = {}, ret = "";
data.replace(/(?:((["'\/]*(("[^"]*")|('[^']*'))?[\s]*)?[\/\/|#][^"|^']*))/g, function(value){
q[key] = value;
});
for ( var key in q ){
ret = q[key];
}
var text = data.split(ret);
var out = ret + text[1];
out = out.replace(/"/g,"""); …Run Code Online (Sandbox Code Playgroud)