2

I have task where I have to open many files, search for //@public object pattern, and if it has no matches, remove all JavaScript comments inside that file that do not have //@public doc before it.

For example, there might be something similar to this:

//@public doc
    /**
     * @function
     * checks for specific conditions
     * specific variables
     */

and that remains as is. But every other JavaScript comment needs to be removed.

What would be regular expression for it, and how to achieve that?

Thank you in advance.

In the meanwhile, I came up to do following solution

    <fileset id="jsmvcdoc.fileset" dir="${doctempfolder}" includes="**/*.js">
        <not>
         <contains text="//@public object" casesensitive="no"/>
        </not>
    </fileset>

   <!-- JS Comments -->
   <replaceregexp match="(?!doc. +)/\*.+?\*/" replace="" flags="gs" byline="false">
        <fileset refid="jsmvcdoc.fileset"/>
   </replaceregexp>

Which filters fileset including only those file that do not contain //@public object, but I dont have idea how to make regexp which will strip all comments except those which are preceeded by line //@public doc

Solved it this way:

<!-- strip out js comments not preceeded by //@public doc -->
  <replaceregexp match="(?&lt;!//@public doc\x0D\x0A    )/\*.+?\*/" replace="" flags="gs" byline="false">
        <fileset refid="jsmvcdoc.fileset"/>
  </replaceregexp>
3
  • To clarify: you need to search each file for the text //@public object, and if that text is not found in the file, you need to remove a) everything that isn’t a JavaScript comment, and b) every JavaScript comment that is not immediately preceded by the text //@public doc. Commented Sep 5, 2012 at 14:21
  • if //@public object is not found in the file I need to remove every JavaScript comment that is not immediately preceded by the text //@public doc Commented Sep 5, 2012 at 14:38
  • Right, gotcha. I’ve amended the wording of your question a little bit to make that clearer. Commented Sep 5, 2012 at 14:42

1 Answer 1

2

I would break it down in this way:

  1. Select files which contain the pattern.
  2. Run replace task on the specific regex.

Something like

<replaceregexp byline="true">
  <regexp pattern="/\*[\d\D]*?\*/"/>
    <substitution expression=""/>
       <fileset dir="${JSfiles.path}" includes="**/*.js">
          <contains text="//@public object" casesensitive="no"/>
       </fileset>
</replaceregexp>

I have not tested it , so you might need to tweak the regex a bit.

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

1 Comment

Thanks Pulak but I need to replace comments only in those files which does not contain //@public object and not immediately preceded by the line //@public doc

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.