0

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

2
  • Since this might be error prone, I would do it one by one :/ Commented Jul 13, 2015 at 10:24
  • @facundofarias: I'm talking from a project with over 1000 files... Commented Jul 13, 2015 at 10:25

1 Answer 1

1

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.

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

Comments

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.