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

557
Visualizações
How do I use regex to convert Slack URLs to BB Code?

I'm trying to use regex to convert Slack's version of markdown formatting to BB Code. I'm stuck on links at the moment. Slack formats like this:

<www.url.com|This is the actual text>
<www.url.com>

BB Code formats like this:

[url=www.url.com]This is the actual text[/url]
[url]www.url.com[/url]

I'm dealing with the first type using this (in javascript)

string.replace(/\<([\s\S]+)(?=\|)\|([\s\S]*?)\>/gm, "[url=$1]$2[/url]"

I'm struggling to make a second rule that will only match text between <...> if there isn't a | in the string. Can anyone help me out?

Also if there's a neat way of dealing with both options in one go then let me know!

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

0

You can use

const text = `<www.url.com|This is the actual text>
<www.url.com>`;
console.log( text.replace(/<([^<>|]*)(?:\|([^<>]*))?>/g, (x, url, text) => text !== undefined ?
 `[url=${url}]${text}[/url]` : `[url]${url}[/url]`) )

See the regex demo. Details:

  • < - a < char (please NEVER escape this char in any regex flavor if you plan to match a < char)
  • ([^<>|]*) - Group 1: any zero or more chars other than <, > and |
  • (?:\|([^<>]*))? - an optional non-capturing group matching one or zero occurrences of a | and then any zero or more chars other than < and > captured into Group 2
  • > - a > char (again, please never escape the > literal char in any regex flavor).
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