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

143
Visualizações
Regex lookahead AND lookbehind

I want to replace all ® signs on my website to an <sup>®</sup> with JavaScript. But I have to care that I only replace them if there is no sup surrounding them already (maybe from CMS users).

I already tried the following but it does not work fine.

html.replace(/(?<!<sup>.{0,})®(?!.{0,}<\/sup>)/g, "<sup>&reg;</sup>")

It does only care for the </sup> Tag and does not work in Safari cause of an "invalid group specifier name". Maybe someone here can help me with this.

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

0

You could use a regex replace with an alternation and callback:

var input = "Hello ® and also <sup>®</sup>";
var output = input.replace(/<sup>®<\/sup>|®/g, (x) => x === "<sup>®</sup>" ? x : "<sup>®</sup>");
console.log(output);

The alternation logic first eagerly attempts to find <sup>®</sup> occurrences. That failing, it also tries to find any other ® occurrence. Then, in the callback, we wrap with <sup> tags only if not already wrapped.

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