-2

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>
Ja͢ck
  • 170,779
  • 38
  • 263
  • 309
Papaya Labs
  • 1,079
  • 9
  • 11
  • Can you show us your operational code? Not sample input. – Halcyon Jun 08 '15 at 20:37
  • The output is every line that is not /** Coment */ replace with . But I need to do it with RegEx. – Papaya Labs Jun 08 '15 at 23:07
  • This question is not duplicate. Is not the same. Who is this Jack??? – Papaya Labs Jun 09 '15 at 02:58
  • You use the other question to filter out all comments, then replace the remaining lines with (code). How hard could that be? – Ja͢ck Jun 09 '15 at 03:08
  • First, I dont want to filter the comments. Just replace what is not comment. Second just the comments /** */ like this. Not all the comments. Third I want to use the function replace and the RegEx, no more javascript code. CLEAR? – Papaya Labs Jun 09 '15 at 03:12
  • I have been working on this code for 2 days. And I already see that question that you say that is duplicate. – Papaya Labs Jun 09 '15 at 03:17
  • Fine, I'll remove the duplicate, but this is a rather low effort question; you could have included a regular expression that can match the kind of comments you're after and optionally some code that's not working. – Ja͢ck Jun 09 '15 at 03:17

3 Answers3

0

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.

WakeskaterX
  • 1,408
  • 9
  • 21
  • Thank u for your answer but I already know this. I want to replace the other code, every line that is not /** Coment */ replaced with – Papaya Labs Jun 08 '15 at 23:05
  • Is the inverse of the RegEx that you suggest – Papaya Labs Jun 08 '15 at 23:08
  • A regex is probably not what you want for this. You should use some sort of file reader and read the file line by line, with a boolean flag if you've hit a comment start, so that you can check for comment ends in subsequent lines. So you would open a file (HTML5 File API) and read each line, do a check for ("/**") in the line and split on that if it exists. If it does not exist, replace that line with (code) in the new file, otherwise write the output of the line you just read if you're in a comment. – WakeskaterX Jun 09 '15 at 13:33
  • Thanks for your reply. I know maybe this not the best way to do it but this time I need to do with RegExs and replace. I think I am very close. – Papaya Labs Jun 09 '15 at 14:03
  • If you were using Regex in another language it might be easier, regex with javascript does not have positive look behind, which would be SUPER helpful in this case. – WakeskaterX Jun 09 '15 at 19:30
  • Hey WakeskaterX, I find the solution with a few lines of code ( The last answer). I will keep in mind all what you guys tell me about javascript and RegEx for future devs. Thanks! – Papaya Labs Jun 09 '15 at 22:22
0

You can use:

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

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

rvazquezglez
  • 2,284
  • 28
  • 40
0

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>
Papaya Labs
  • 1,079
  • 9
  • 11