-1

I am using TypeScript and I want to remove all the comments from the code using regex. Here's my sample code:

// this is awesome code
const a =1

// this is nice
const b = 2 // oh great


  // with some indentation
const x = 99

I want to preserve the new lines as it is and want to remove all the lines so final output should look like the below:

const a =1

const b = 2


const x = 99

To achieve above I have to write regex: ^( *\/\/.*)\n|( *\/\/.*)

The question is how can I backreference the first group after or | condition? I can't do ^( *\/\/.*)\n|(\1).

P.S. I am using regex101.com for testing

Thanks.

anubhava
  • 761,203
  • 64
  • 569
  • 643
JD Solanki
  • 898
  • 8
  • 18

1 Answers1

2

You can search using this regex:

^[ \t]*\/\/.*\r?\n|[ \t]*\/\/.*

And replace with empty string.

RegEx Demo

RegEx Details:

  • ^: Start
  • [ \t]*: Match 0 or more horizontal whitespaces
  • \/\/: Match //
  • .*: Match everything till end of line
  • \r?\n: Match optional carriage return followed by line feed character
  • |: OR
  • [ \t]*: Match 0 or more horizontal whitespaces
  • \/\/: Match //
  • .*: Match everything till end of line
anubhava
  • 761,203
  • 64
  • 569
  • 643
  • Almost there. But this regex removes the two lines between `const b` and `const x`. We just got single line between both `const`. – JD Solanki Dec 01 '22 at 06:11
  • Moreover, the question is regarding backreferencing groups with or. – JD Solanki Dec 01 '22 at 06:14
  • check my updated answer that preserves line breaks between `const b` and `const x`. You don't need to back-reference any groups since you just intend to remove matched parts. – anubhava Dec 01 '22 at 06:19
  • btw if you're using Javascript you may also use this regex: `[ \t]*\/\/.*(?:(?<=^[ \t]*\/\/.*)\r?\n)?` – anubhava Dec 01 '22 at 06:24
  • @JDSolanki: If I understand you correctly you wanted to avoid repeating `[ \t]*\/\/.*` on both sides of `|`. This is not back-reference. A back-reference matches same captured value but it doesn't repeat same regex subpattern. That feature is available in PCRE but not in Javascript. btw in php/PCRE this regex can be simplified to: `(^)?[ \t]*\/\/.*(?(1)\r?\n)` – anubhava Dec 01 '22 at 06:36
  • Hi @anubhava. I have a few questions if you don't mind. 1) If this is not called backreference then what it is called? 2) Can you please update your answer to explain JS regex you mentioned in the above comment? 3) Assume if I have `hello123-123` and I have regex `(hello)(123)-(\2)` then I can omit to repeat the second group using `\2` in JS so why it doesn't support with or? Thanks. – JD Solanki Dec 01 '22 at 08:22
  • Yes you can use it here because `123` is same value. However if you want to match `hello123-789` then you cannot use: `(hello)(\d+)-(\2)` – anubhava Dec 01 '22 at 08:27