-1

Basically what I want to do is parse code, and remove all comments made with "//", including the "//", until a new line appears. I don't know how to effectivly do it in Regex unfortunately.

Some example code might look like this:

variable += 10; //comment to be removed.
more code...

so only "//comment to be removed." gets removed

Elias
  • 149
  • 1
  • 9

2 Answers2

2

console.log(`something // awodkajwodkjoawjdojawdjk
another thing // fgskgkjhgkf
last thing`.replace(/\/\/.*/g, ''));
Rhumborl
  • 16,349
  • 4
  • 39
  • 45
Karen Grigoryan
  • 5,234
  • 2
  • 21
  • 35
  • @pillikasnazsaxc you are welcome. Please feel free to upvote and [accept](https://i.stack.imgur.com/LkiIZ.png) the answer. Thanks. – Karen Grigoryan May 31 '17 at 20:42
0

This will allow you to parse the code, instead of just removing the comments from a string:

var code = getCode();
var stripped = '';
var regex = /(.*)\/\/.*/g;
var result;
while ( result = regex.exec( code ) ) {
    console.log( result );
    // Do other stuff with line of code
    stripped += ( result[1] + '\n' );
}
TbWill4321
  • 8,626
  • 3
  • 27
  • 25