类型为//的干净 JavaScript 注释的正则表达式 [英] Regular expression for clean javascript comments of type //

查看:12
本文介绍了类型为//的干净 JavaScript 注释的正则表达式的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用以下 REGEXP:

I´m using the following REGEXP:

$output = preg_replace( "///(.*)\n/", "", $output );

代码运行良好但是!!!!,当 URL 像 (http://this_is_not_a_comment.com/kickme) 时,代码会替换它... (http://)

The code works well BUT!!!!, when a URL like (http://this_is_not_a_comment.com/kickme), the code replaces it... (http://)

你能做些什么来不替换这些 URL.

What can you do to no replace that URLs.

谢谢,

推荐答案

您需要一个可以区分代码和注释的正则表达式.特别是,由于//的序列既可以在字符串中,也可以在注释中,因此只需区分字符串和注释即可.

You need a regular expression that can distinguish between the code and the comments. In particular, since the sequence of // can either be in a string or a comment, you just need to distinguish between strings and comments.

这是一个可能会执行此操作的示例:

Here’s an example that might do this:

/(?:([^/"']+|/*(?:[^*]|*+[^*/])**+/|"(?:[^"\]|\.)*"|'(?:[^'\]|\.)*')|//.*)/

在替换函数中使用它,同时用第一个子模式的匹配替换匹配的字符串,然后应该能够删除 // 样式注释.

Using this in a replace function while replacing the matched string with the match of the first subpattern should then be able to remove the // style comments.

一些解释:

  • [^/"']+ 匹配任何不是注释开头的字符(//…>/*...*/) 或一个字符串
  • /*(?:[^*]|*+[^*/])**+/ 匹配 /* ...*/ 样式注释
  • "(?:[^"\]|\.)*" 匹配双引号中的字符串
  • '(?:[^'\]|\.)*' 匹配单引号中的字符串
  • //.* 最终匹配 //... 样式注释.
  • [^/"']+ matches any character that is not the begin of a comment (both //… and /*…*/) or of a string
  • /*(?:[^*]|*+[^*/])**+/ matches the /* … */ style comments
  • "(?:[^"\]|\.)*" matches a string in double quotes
  • '(?:[^'\]|\.)*' matches a string in single quotes
  • //.* finally matches the //… style comments.

由于前三个结构被分组在一个捕获组中,匹配的字符串可用并且当用第一个子模式的匹配替换匹配的字符串时没有任何改变.仅当 //... 样式的注释匹配时,第一个子模式的匹配项为空,因此它被替换为空字符串.

As the first three constructs are grouped in a capturing group, the matched string is available and nothing is changed when replacing the matched string with the match of the first subpattern. Only if a //… style comment is matched the match of the first subpattern is empty and thus it’s replaced by an empty string.

但请注意,这可能会失败.我不太确定它是否适用于任何输入.

But note that this may fail. I’m not quite sure if it works for any input.

这篇关于类型为//的干净 JavaScript 注释的正则表达式的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆