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

279
Visualizações
How can I replace @ symbol just from href?

I have a condition where I wrap the @username values in an anchor to make them clickable

const text = 'test @username'
const replaced = text.replace(/(@[a-z_\d]+)/ig, '<a class="tag_user" href="/profile/$1" >$1</a> ')
console.log(replaced)

Currently, the output was like

test <a class="tag_user" href="/profile/@username" >@username</a>

And I want to achieve something like:

test <a class="tag_user" href="/profile/username" >@username</a>

Can anybody have any idea how can I achieve the expected output?

about 4 years ago · Juan Pablo Isaza
2 Respostas
Responde à pergunta

0

The simplest solution is probably to remove @ from the capture group. So @(...) instead of (@...).

Then add the @ as a literal inside the replacement string wherever it is needed. Meaning that /profile/$1 stays the same, but the anchor contents becomes @$1.

Alternatively you could use $& (whole match) instead of @$1.

const text = 'test @username'

const a = text.replace(/@([a-z_\d]+)/ig, '<a class="tag_user" href="/profile/$1" >@$1</a> ')
console.log(a)

const b = text.replace(/@([a-z_\d]+)/ig, '<a class="tag_user" href="/profile/$1" >$&</a> ')
console.log(b)

about 4 years ago · Juan Pablo Isaza Relatório

0

You could capture the @ and username separately.

const text = 'test @username'
const replaced = text.replace(/(@)([a-z_\d]+)/ig, '<a class="tag_user" href="/profile/$2" >$1$2</a> ')
console.log(replaced)

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