我需要匹配所有这些开始标记:
<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)
我相信它说:
/,然后我有这个权利吗?更重要的是,你怎么看?
我在我的正则表达式身体中发现了这些东西,但我不知道我能用它们做什么.有人有例子,所以我可以尝试了解它们是如何工作的吗?
(?!) - negative lookahead
(?=) - positive lookahead
(?<=) - positive lookbehind
(?<!) - negative lookbehind
(?>) - atomic group
Run Code Online (Sandbox Code Playgroud) 我从Word生成了一些丑陋的HTML,我想从中删除所有HTML注释.
HTML看起来像这样:
<!--[if gte mso 9]><xml> <o:OfficeDocumentSettings> <o:RelyOnVML/> <o:AllowPNG/> </o:OfficeDocumentSettings> </xml><![endif]--><!--[if gte mso 9]><xml> <w:WordDocument> <w:View>Normal</w:View> <w:Zoom>0</w:Zoom> <w:TrackMoves/> <w:TrackFormatting/> <w:HyphenationZone>21</w:HyphenationZone> <w:PunctuationKerning/> <w:ValidateAgainstSchemas/> <w:SaveIfXMLInvalid>false</w:SaveIfXMLInvalid> <w:IgnoreMixedContent>false</w:IgnoreMixedContent> <w:AlwaysShowPlaceholderText>false</w:AlwaysShowPlaceholderText> <w:DoNotPromoteQF/> <w:LidThemeOther>NO-BOK</w:LidThemeOther> <w:LidThemeAsian>X-NONE</w:LidThemeAsian> <w:LidThemeComplexScript>X-NONE</w:LidThemeComplexScript> <w:Compatibility> <w:BreakWrappedTables/> <w:SnapToGridInCell/> <w:WrapTextWithPunct/> <w:UseAsianBreakRules/> <w:DontGrowAutofit/> <w:SplitPgBreakAndParaMark/> <w:EnableOpenTypeKerning/> <w:DontFlipMirrorIndents/> <w:OverrideTableStyleHps/> </w:Compatibility> <m:mathPr> <m:mathFont m:val="Cambria Math"/> <m:brkBin m:val="before"/> <m:brkBinSub m:val="--"/> <m:smallFrac m:val="off"/> <m:dispDef/> <m:lMargin m:val="0"/> <m:rMargin m:val="0"/> <m:defJc m:val="centerGroup"/> <m:wrapIndent m:val="1440"/> <m:intLim m:val="subSup"/> <m:naryLim m:val="undOvr"/> </m:mathPr></w:WordDocument> </xml><![endif]-->
Run Code Online (Sandbox Code Playgroud)
..我正在使用的正则表达式就是这个
html = html.replace(/<!--(.*?)-->/gm, "")
Run Code Online (Sandbox Code Playgroud)
但似乎没有匹配,字符串不变.
我错过了什么?
我有一个功能,例如
var test = function () {alert(1);}
Run Code Online (Sandbox Code Playgroud)
我怎样才能获得这个功能的主体?
我假设唯一的方法是解析test.toString()方法的结果,但还有其他方法吗?如果解析是唯一的方法,那么正则表达式将是什么?(极端需要帮助正则表达式,因为我不熟悉它们)
如何使用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 = …
我正在使用javascript对javascript代码进行粗略的解析.我会不遗余力的细节,为什么我需要这样做,但我只想说,我不希望集成的代码库一大块,因为它是不需要我的目的,我保持这种非常轻便是非常重要的而且相对简单.所以请不要建议我使用JsLint或类似的东西.如果答案的代码多于您可以粘贴到答案中的代码,那么它可能比我想要的更多.
我的代码目前能够很好地检测引用的部分和注释,然后匹配括号,括号和parens(当然,确保不要被引号和注释混淆,或者在引号内转义).这就是我需要它做的全部,并且它做得很好......但有一个例外:
它可能会被正则表达式文字混淆.所以我希望在javascript字符串中检测正则表达式文字有一些帮助,所以我可以适当地处理它们.
像这样的东西:
function getRegExpLiterals (stringOfJavascriptCode) {
var output = [];
// todo!
return output;
}
var jsString = "var regexp1 = /abcd/g, regexp1 = /efg/;"
console.log (getRegExpLiterals (jsString));
// should print:
// [{startIndex: 13, length: 7}, {startIndex: 32, length: 5}]
Run Code Online (Sandbox Code Playgroud) 有一些字符串(例如,“ s”):
import 'lodash';
// TODO import jquery
//import 'jquery';
/*
Some very important comment
*/
Run Code Online (Sandbox Code Playgroud)
如何删除“ s”字符串中的所有注释?我应该使用一些正则表达式吗?我不知道。
我将所有javascriupt组合成一个整齐的文件,以降低http请求!我坚持删除评论/* comments */和// comments.我的水平远远低于缩小或解析的东西.我知道如何制作通心粉串.任何比这更复杂的东西,你在我的电脑或厨房里都找不到,所以:
同时将它合并到一个文件,我想删除所有评论.
什么是正确的正则表达式?
<?php
header('Content-type: text/javascript');
$offset = 60 * 60 * 24; // Cache for a day
header ('Cache-Control: max-age=' . $offset . ', must-revalidate');
header ('Expires: ' . gmdate ("D, d M Y H:i:s", time() + $offset) . ' GMT');
ob_start("compress");
function compress($buffer) {
# NOT SURE, not all new lines are removed??
# remove tabs, spaces, newlines, etc.
$buffer = str_replace(array("\r\n", "\r", "\t", ' ', ' '), '', $buffer); …Run Code Online (Sandbox Code Playgroud) 我使用以下REGEXP:
$output = preg_replace( "/\/\/(.*)\\n/", "", $output );
Run Code Online (Sandbox Code Playgroud)
代码运行良好但是!!!!,当像(http://this_is_not_a_comment.com/kickme)这样的URL时,代码替换它...(http://)
你怎么做才能取代那些网址.
谢谢,
javascript ×7
regex ×7
php ×3
html ×2
lookaround ×1
parsing ×1
preg-replace ×1
text ×1
xhtml ×1