我需要匹配所有这些开始标记:
<p>
<a href="foo">
Run Code Online (Sandbox Code Playgroud)
但不是这些:
<br />
<hr class="foo" />
Run Code Online (Sandbox Code Playgroud)
我想出了这个,并希望确保我做对了.我只抓住了a-z.
<([a-z]+) *[^/]*?>
Run Code Online (Sandbox Code Playgroud)
我相信它说:
/,然后我有这个权利吗?更重要的是,你怎么看?
我需要使用JavaScript RegExp对象从JavaScript源中删除所有JavaScript注释.
我需要的是RegExp的模式.
到目前为止,我发现了这个:
compressed = compressed.replace(/\/\*.+?\*\/|\/\/.*(?=[\n\r])/g, '');
Run Code Online (Sandbox Code Playgroud)
此模式适用于:
/* I'm a comment */
Run Code Online (Sandbox Code Playgroud)
或者:
/*
* I'm a comment aswell
*/
Run Code Online (Sandbox Code Playgroud)
但似乎不适用于内联:
// I'm an inline comment
Run Code Online (Sandbox Code Playgroud)
我不是RegEx的专家和它的模式,所以我需要帮助.
另外,我想要一个RegEx模式,它将删除所有那些类似HTML的注释.
<!-- HTML Comment //--> or <!-- HTML Comment -->
Run Code Online (Sandbox Code Playgroud)
还有那些条件HTML注释,可以在各种JavaScript源中找到.
谢谢.
如何使用PHP删除JS注释?这个问题已更新:2013年11月4日回答:Alexander Yancharuk但是现在有一个问题.新代码:id = id.replace(/\//g,'');
这是我的例子:
<?php
$output = "
//remove comment
this1 //remove comment
this2 /* remove comment */
this3 /* remove
comment */
this4 /* * * remove
* * * *
comment * * */
this5 http://removecomment.com
id = id.replace(/\//g,''); //do not remove the regex //
";
$output = preg_replace( "/(?:(?:\/\*(?:[^*]|(?:\*+[^*\/]))*\*+\/)|(?:(?<!\:)\/\/.*))/", "", $output ); //Yancharuk's code/regex
// "/(?<!\:)\/\/(.*)\\n/ = my oldest code
echo nl2br($output);
?>
Run Code Online (Sandbox Code Playgroud)
我的问题;
这是输出,最近:
this1
this2
this3
this4
this5 http://removecomment.com
ID = …
我需要匹配并替换一些评论.例如:
$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)