0

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.

For example // some comment goes here to be replaced with /* some comment goes here */

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

Problems:

  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.

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

1
  • What about: myString = ' // ';? Commented Oct 14, 2014 at 9:42

2 Answers 2

3

Quick way

Use this regex:

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

Replace with:

/\*$1\*/

Explanatory way

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 - 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.

* I don't confirm this searching and replacing method, however it may work with most cases.

Update

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

enter image description here

Then do a Replace All

enter image description here

Sign up to request clarification or add additional context in comments.

6 Comments

Thanks Revo. Notepad++ has some odd behaviour, it seems to intermittently not find/replace the same \\ comment I will need further experimentation on this. How can your regex be modified so that a non white space character in front of the \\ will still find the comment. I have tried a period and that finds such a comment but also finds URLS and DOCTYPE but will not find a comment is there is a new line in front of it.please have a look at regex101.com/r/xG4eQ5/2
@user1558796 When using online Regular Expression testers you have to use g flag to see all occurrences, Check it. Also in Notepad++ there is no need to escape slashes. I updated my answer with pictures.
thanks revo, you have put quite a lot of work into this. If I have a // type comment that is butting up to a ; or an other character then the search replace will not work. Is there any workaround for that? Thanks again.
@user1558796 Would you give an example in which it doesn't work?
revo, in this regex101.com/r/xG4eQ5/5 the line is not found ;//comment but //comment is.
|
1

This is not a job for regex, use a parser instead. Have a look at: Esprima for example.

1 Comment

M42, Esprima is very interesting, I had not heard of it before. However I did not find a way to use it to change // comments into /* */ comments. Thank you

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.