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

199
Vistas
Match URL's from string but exclude image url's and document url's

Currently i'm building a function for a messaging system that detects a url from the string and add anchor tag to it. But i need to exclude the urls that points to images and documents. The function that i created now detects all the urls and add anchor tag to it but it adds anchor tag to images and documents also.

i tried these regex but no desired result

/(.[^>"]|[^=]")\b(https?|ftp|file)/gi

/(https?:\/\/[^\s]+)/g

    function urlParsing(text) {
      var urlRegex = /(https?:\/\/[^\s]+)/g;
      return text.replace(urlRegex, function(url) {
       return '<a href="' + url + '">' + url + '</a>';
      })
    }

    console.log(urlParsing('Hello john https://stackoverflow.com'));

    console.log(urlParsing('Hello john https://picsum.photos/200/300.jpg'));

about 4 years ago · Juan Pablo Isaza
1 Respuestas
Responde la pregunta

0

Add a negative lookahead to your regex. This is the (?!) group, and it means match as long as whatever comes before isn't followed by anything in the lookahead. Then add a check to test the text against that parser and return from the function if the test fails.

function urlParsing(text) {
      var urlRegex = /(https?:\/\/(?!.*\.(jpg|png|gif|pdf|docx))[^\s]+)/g;
      if(!urlRegex.test(text)) {return false};
      return text.replace(urlRegex, function(url) {
       return '<a href="' + url + '">' + url + '</a>';
      })
    }

    console.log(urlParsing('Hello john https://stackoverflow.com'));
    console.log(urlParsing('Hello john https://pbs.org'));

    console.log(urlParsing('Hello john https://picsum.photos/200/300.jpg'));
    console.log(urlParsing('Hello john https://picsum.photos/200/300.jpg?arg=x&arg2=y'));
    console.log(urlParsing('Hello john https://picsum.photos/200/300.png'));
    console.log(urlParsing('Hello john https://picsum.photos/200/300.gif'));
    console.log(urlParsing('Hello john https://picsum.photos/200/300.pdf'));
    console.log(urlParsing('Hello john https://picsum.photos/200/300.docx'));

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