Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

225
Views
Regex pattern to match comments, but not URLs

I am trying to create a regex for detecting all the comments in a code. But I am also getting the hyperlinks because of the inline comment characters sequence //, which I don't want; I just need the comment part.

var sample = `Hello //this is a test
 http://google.com
 //second
 third
 https://test.com`;
var regex = new RegExp(
  /(\/\*([^*|[\r\n]|(\*+([^*\/]|[\r\n])))*\*+\/)|(\/\/.*)/g
);
console.log(sample.match(regex));

The output is ["//this is a test", "//google.com", "//second", "//test.com"]
The output should be ["//this is a test", "//second"]

about 4 years ago · Juan Pablo Isaza
2 answers
Answer question

0

For a quick and dirty way, you could use

(?<=^|[^\S])\/\/.+

See a demo on regex101.com.


In JavaScript, this could be

var sample = `Hello //this is a test
 http://google.com
 //second
 third
 https://test.com`;
var regex = new RegExp(/(?<=^|[^\S])\/\/.+/g);
console.log(sample.match(regex));

about 4 years ago · Juan Pablo Isaza Report

0

I added newline and 0 or more tabs before your two main capture groups:

/[\r\n][/t]*(\/\*([^*|[\r\n]|(\*+([^*\/]|[\r\n])))*\*\/)|[\r\n][\t]*(\/\/.*)/g

This means, the comment has to be at the beginning of a new line, so it wont match a hyperlink in the middle of your code.

about 4 years ago · Juan Pablo Isaza Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!