RegEx for all the javascript code except comments

134 Views Asked by At

What I am trying to do is to select every line of javascript code except for the comments (just the comments /** to */ with or without spaces in the beginnig) and replace with the word (code). I need it with a RegEx and replace javascript function.

For example

Input

/** 
* Source Code
*/

  /**
  * Places given shatter objects images into the specified dom element
  *
  * @param {object} shatter - Shatter object
  * @param {object} domElement - The dom element to append images to
  */

function placeShatter (shatter, domElement) {
  // adjustment to center image on screen
  var adjustment = (window.innerWidth / 2) * image.width / 2;
  for (var i = 0; i < shatter.images.length; i++) {
    placeImageAbsolute(shatter.images[i].image,
                       domElement, 
                       shatter.images[i].x, 
                       shatter.images[i].y,
                       adjustment,
                       YLOC);
   } 
}

Output

/** 
* Source Code
*/

  /**
  * Places given shatter objects images into the specified dom element
  *
  * @param {object} shatter - Shatter object
  * @param {object} domElement - The dom element to append images to
  */
(code)
(code)
(code)
(code)
(code)
(code)
(code)
(code)
(code)
(code)
(code)
(code)
(code)

The RegEx has to work with this code:

  <!DOCTYPE html>
  <html>
  <body>

  <p>Source Code</p>
  <textarea rows="50" cols="150" id="input"></textarea>

  <p>Click the button to perfom a global replacement and display   the matches.</p><button onclick="myFunction()">Try it</button>
  </br>
  <textarea rows="50" cols="150" id="output"></textarea>
  <script>
    function myFunction() {
      var not_comments = document.getElementById("input").value.replace(RegEX,'(code)');
      document.getElementById("output").value = not_comments;
    }
  </script>

  </body>
  </html>
3

There are 3 best solutions below

6
On

If you want to just eliminate it, you can capture it and do a replace.

For Example:

var text = "/** TEST */ stuff";
text.replace(/(\/\*\*[\s\S]*\*\/)/g, '');

To Break it Down:

(\/\*\*            //This Matches /** with the start of a capture group
[\s\S]*            //This Matches ANY character (including line endings) zero or more times.
\*\/)              //This matches */ and closes the group

that would take that text and replace the /** TEST */ with a blank space.

Here is an example: https://regex101.com/r/xA1yY5/1

BUT, if you're dealing with large files, you're better off reading the file with a file reader and writing to a new file and just not including the comments. This can be done by creating a flag that turns on when you hit /** and doesn't allow writing and then turns off if it hits */ and allows writing again.

0
On

You can use:

\*\/([\s\S]*?)(\/\*\*|$)

Here is the example: https://regex101.com/r/qJ0eQ2/1

0
On

Here is my final approach:

<!DOCTYPE html>
<html>
<body>

<p>Source Code</p>
<textarea rows="50" cols="150" id="input"></textarea>

<p>Click the button to perfom a global replacement and display the matches.
</p><button onclick="myFunction()">Try it</button>
</br>
<p>Output</p>
<textarea rows="50" cols="150" id="output"></textarea>
<p>Output without blank lines</p>
<textarea rows="50" cols="150" id="output_without_blank_lines"></textarea>

<script>
function myFunction() {
 var code = "(code)"
 var withoutspaces = document.getElementById("input").value.replace(/^ +/gm, '');
 var regex = /\/\*[\s\S]*?\*\/|(^[\s\S]*?$)/gm;
 var replaced = withoutspaces.replace(regex, function(m, group1) {
         if (group1) return code;
         else return m;
     });
    document.getElementById("output").value = replaced;
    document.getElementById("output_without_blank_lines").value = replaced.replace(/^[ \t]*$\r?\n/gm,code+'\n');
}
</script>

</body>
</html>