Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

224
Vistas
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 Respuestas
Responde la pregunta

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 Denunciar

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 Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda