Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

110
Visualizações
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 Respostas
Responde à pergunta

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 Relatório

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 Relatório

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 Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda