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

111
Vistas
JavaScript function to identify URL and make them clickable

I have a requirement , my client send me a string. for the links he is sending link title in squre brackets and link with bracket. like below,

[Google](https://www.google.com/)

I need get that value and make it clickable Google . adding like below and replace that to the original text.

<a href = "' + title + '" target="_blank">' + url + '</a>'

can anyone suggest better way of doing this with JavaScript regex.

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

0

Looks like Markdown formatting, so you could use a markdown library like Marked to parse and render it:

const s = '[Google](https://www.google.com/) ';

document.getElementById('content').innerHTML = marked(s);
<script src="https://cdn.jsdelivr.net/npm/marked/marked.min.js"></script>
<div id="content"></div>

about 4 years ago · Juan Pablo Isaza Denunciar

0

Can be done with String replace function.

Regex: /\[(.*)\]\s*\((.*)\)/g

Replacer: <a href = "$2" target="_blank">$1</a>

const str = `Lorem ipsum. [Google](https://www.google.com/). Sample text.`

const output = replaceWithLinks(str);

console.log(output);

function replaceWithLinks(str) {
    return str.replace(/\[(.*)\]\s*\((.*)\)/g, '<a href="$2" target="_blank">$1</a>')
}

about 4 years ago · Juan Pablo Isaza Denunciar

0

In HTML file:

<h1 id="header"></h1>

In Js File:

const myString = "[Google](https://www.google.com/)";
const show = myString.match(/\[(.*?)\]/); // it return two things. The first is with bracket and the second one is without bracket you have to use without bracket.
const url = myString.match(/\((.*?)\)/);
document.getElementById("header").innerHTML = `<a href="${url[1]}" target="_blank">${show[1]}</a>`;

You have to use regular expression. To get information about regular expression read MDN.

Get the first index value and show it to the UI.

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