Dinistro
Dinistro

Reputation: 5730

Replace JS comments in eclipse

I'm recently working on an old project where we're using jsp in the front-end. My actual job is to remove all js-comments (//) and replace them with jsp-comments (<%-- --%>) in the .jsp-files to reduce traffic. My IDE is eclipse, so I'm using search and replace over multiple files with regex (Ctrl+H).

Note

I've already removed all /* */ comments in all files.

For example some code we could find in a jsp-file:

<script type="text/javascript">
<!--
    function submitSave() {
        // Coment in JS
        if (doubleClick()) { return; }

        document.benutzerRollenFunktionenForm.action = '<%=request.getContextPath()%>/administration/benutzerRollenFunktionenBearbeiten.do';

        <% if (request.getAttribute("methode") != null && request.getAttribute("methode").toString().trim().equals("benutzerSpeichern")) { //%>
            document.benutzerRollenFunktionenForm.method.value = 'benutzerSpeichern';
        <% } else if (request.getAttribute("methode") != null && request.getAttribute("methode").toString().trim().equals("benutzerNeuAnmeldungSpeichern")) { %>
            document.benutzerRollenFunktionenForm.method.value = 'benutzerNeuAnmeldungSpeichern';
        <% } // End If %>
    }
//-->
</script>

As you can see, there are also comments inside the jsp-tags, so I can't just search for // and remove them.

I can match all comments that aren't in a oneliner-jsp-tag with this regex:

^((?:(?!<%).)*)(\/\/)((?:(?!-->|%>).)*)$

Note

--> is for preventing to match //--> because this isn't a normal comment an needs to remain.

But then there is also code like this:

<%
  JAVA CODE
  //Comment
%>

Does someone have a solution to match all the comments in the JS to replace them with-jsp comments?

EDIT

Unfortunately I can't do this all by hand, because there are more than 1000 files and much more than 1000 comments...

Upvotes: 0

Views: 154

Answers (1)

bluedevil2k
bluedevil2k

Reputation: 9491

Write a program that will read the file in line by line. As you traverse the lines, turn a flag to true when you encounter the . Then, when you encounter a // and the flag is false, replace the // with a . When the flag is true, don't do that.

Upvotes: 1

Related Questions