replace single line javascript comments with multiline style comments in Notepad++ using regular expressions

前端 未结 2 485
你的背包
你的背包 2021-01-26 05:43

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.

相关标签:
2条回答
  • 2021-01-26 06:19

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

    0 讨论(0)
  • 2021-01-26 06:38

    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

    0 讨论(0)
提交回复
热议问题