• 首页 首页 icon
  • 工具库 工具库 icon
    • IP查询 IP查询 icon
  • 内容库 内容库 icon
    • 快讯库 快讯库 icon
    • 精品库 精品库 icon
    • 问答库 问答库 icon
  • 更多 更多 icon
    • 服务条款 服务条款 icon

类型为//的干净 JavaScript 注释的正则表达式

用户头像
it1352
帮助6

问题说明

我正在使用以下 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.

谢谢,

正确答案

#1

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

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.

这篇好文章是转载于:学新通技术网

  • 版权申明: 本站部分内容来自互联网,仅供学习及演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,请提供相关证据及您的身份证明,我们将在收到邮件后48小时内删除。
  • 本站站名: 学新通技术网
  • 本文地址: /reply/detail/tanghfabk
系列文章
更多 icon
同类精品
更多 icon
继续加载