使用正则表达式在Notepad ++中用多行样式注释替换单行javascript注释 [英] replace single line javascript comments with multiline style comments in Notepad++ using regular expressions

查看:155
本文介绍了使用正则表达式在Notepad ++中用多行样式注释替换单行javascript注释的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想使用Notepad ++搜索javascript文件或包含某些javascript的html文件,并用多行样式注释替换所有单行注释。

I would like to use Notepad++ to search a javascript file or a html file containing some javascript and replace all single line comments with a multiline style comment.

例如 //此处有一些评论要替换为 / *此处有一些评论* /

使用Notepad ++搜索并替换为用(//.*)(\r\n)选择的正则表达式进行搜索,并 \ / * \1\ * / \r\n 有点用。

Using Notepad++ search and replace with Regular Expression selected with (//.*)(\r\n) for search and \/*\1\*/\r\n kinda works.

问题:


  1. 如果至少有一个空格,它只会找到 //此处有一些评论 //之前,如果在它之前,或者在行的开头,或者如果前面有字母/数字,则找不到它。我可以通过首先执行全局非正则表达式搜索替换来替换所有出现的 // space //

  2. //此处添加一些注释替换为 / * //此处添加一些注释* / 不替换两个正斜杠。之后,我可以通过执行全局非正则表达式搜索来解决此问题,方法是将所有出现的 / * // 替换为 / *

  3. JavaScript可能位于html文件中,在这种情况下,文件中的某处可能会出现 http://msdn.microsoft之类的内容。 com / 显然我不希望将它替换为 http:/ * msdn.microsoft.com / * / 我可以提前解决此问题通过使用:/ ZZZ / 替换所有:// ,其中ZZZ是某种转义方法,然后替换:/ ZZZ / ://

  4. 喜欢会出现问题的<!DOCTYPE HTML PUBLIC-// W3C // DTD HTML 4.01 Transitional // EN> 我想我将不得不手动照顾这些。 / li>
  1. It only finds // some comment goes here if there is at least one space before the // it will not find it if there is a tab before it, or at the start of a line or if there is a letter/number before it. I could workaround that by first doing a global non regular expression search replace to replace all occurrences of // with space //
  2. // some comment goes here is replaced with /*// some comment goes here*/ that is the two forward slashes are not replaced. I can workaround this afterwards by doing a global non regular expression search to replace all occurrences of /*// with /*.
  3. The javascript may be in a html file, in which case somewhere in the file there is likely to be something like http://msdn.microsoft.com/ clearly I would not like this to be replaced with http:/*msdn.microsoft.com/*/ I could workaround this in advance by replacing all :// with say :/ZZZ/ where ZZZ is some escaping method and then afterwards replacing :/ZZZ/ with ://
  4. There will be problems with the likes of <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"> I guess that I will have to look after these manually.

这实际上不是Notepad ++问题。我确信使用任何常规的搜索和替换系统都将遇到同样的困难。

This is not really a Notepad++ problem. I am sure that I would have the same difficulties using any regular search and replace system.

所有建议都将受到感激。

All suggestions gratefully received.

谢谢您抽出宝贵的时间阅读

Thank you for taking the time to read this

推荐答案

快速方法

使用此正则表达式:

(?<=\s)//([^\n\r]*)

替换为:

/\*$1\*/

说明方式

1-双斜杠 // 不会被替换,因为您有他们在那个捕获组中。因此,您将再次捕获并替换它们。

1 - Double slashes // weren't going to replace, because you had them in that capturing group. So you'll capture and replace them again.

2-大多数情况下,注释开始前只有空格或换行符(\n)。我将其包括在内以确保实现。这样, URLs DOCTYPE 不会被触碰。

2 - Most of the time there is nothing but a space or a newline (\n) before comments begin. I included this as a lookbehind to ensure. By this way, URLs and DOCTYPE won't get touched.

*我不确认此搜索,并且

请注意设置。您应该将光标放在文件内容的开头。

Take care of settings. You should have you cursor at the very beginning of the file content.

然后执行全部替换

这篇关于使用正则表达式在Notepad ++中用多行样式注释替换单行javascript注释的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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