简体   繁体   English

注释掉正则表达式,javascript

[英]Commenting out regex, javascript

Apologies if this has already been covered - perhaps i am searching for the wrong thing.抱歉,如果这已经被覆盖 - 也许我正在寻找错误的东西。

The following commented out line generates a syntax error:以下注释掉的行会产生语法错误:

/* /[foo].*/ */

It seems that the '*/' within the regex is confused with the end of the comment.正则表达式中的 '*/' 似乎与评论的结尾混淆了。

Is there a way to safely comment out blocks of code containing regular expressions?有没有办法安全地注释掉包含正则表达式的代码块?

This subject rises in many programming languages.这个主题出现在许多编程语言中。

I mark places that look like comment ends with special chars, so I can easily find them later to undo the comment.我用特殊字符标记看起来像评论结尾的地方,以便以后可以轻松找到它们以撤消评论。 First it looks like this:首先它看起来像这样:

 /* comment start here var emailBlabla = someString.replace(/^\s*/, '').replace(/\s*$/, ''); comment ends here */

Then:然后:

 /* comment start here var emailBlabla = someString.replace(/^\sx*x/x, '').replace(/\s*$/, ''); // <<< note x*x/x comment ends here */

Now whenever I want to uncomment, I just look for the sequence x*x/x.现在,每当我想取消注释时,我只需查找序列 x*x/x。

Note that this doesn't work with tags, eg context Fluid/TYPO3:请注意,这不适用于标签,例如上下文 Fluid/TYPO3:

 <f:comment>

If you want to "deactivate" this tag without removing it, this won't work:如果你想“停用”这个标签而不删除它,这将不起作用:

 x<fx:xcomment>

Because the parser still sees a tag with namespace, and the namespace just changed to fx.因为解析器仍然看到一个带有命名空间的标签,而命名空间只是更改为 fx。 In the context of tags, you have to break with space.在标签的上下文中,你必须打破空间。

 x < fx: xcomment>

But even then you have a clearly marked situation where you can work with find/replace.但即便如此,您也有一个明确标记的情况,您可以在其中使用查找/替换。

It's an error because you can't include */ in the comment, otherwise it terminates the comment and then the last */ is left by itself to error out.这是一个错误,因为您不能在注释中包含*/ ,否则它会终止注释,然后最后一个*/会自行出错。 Just add a space:只需添加一个空格:

 /* /[foo].* / */

Or comment out the WHOLE line:或注释掉整行:

 // /[foo].*/

If you are possessing a string with code in it and adding comment blocks programmatically, then simply replace all */ with * / :如果您拥有 string 并以编程方式添加注释块,则只需将所有*/替换为* /

 var final = '/*' + code.replace(/\*\//g, '* /') + '*/';

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM