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

251
Visualizações
How to add a condition to the following replace?

I want to replace all straight double quotes with curly double quotes:

const text = `"This has an opening and a closing quote."

"This only has an opening quotes

This doesn't have quotes.`

const result = text.replace(/"([^"\n\r]*)"?/g, '“$1”')

console.log(result)

The problem with my current .replace is that it's adding curly double quotes when there aren't closing straight double quotes:

“This has an opening and a closing quote.”

“This only has an opening quotes”

This doesn't have quotes.

How to change my .replace function so it doesn't add curly double closing quotes when there are no straight double closing quotes?

Expected output:

“This has an opening and a closing quote.”

“This only has an opening quotes

This doesn't have quotes.

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

0

Use Unicode rather than the HTML entities. Like so:

const text = `"This has an opening and a closing quote."

"This only has an opening quotes

This doesn't have quotes.`

const result = text.replace(/"([^"\n\r]*)"?/g, '\u201d$1\u201c')

console.log(result)

Where \u201d is the opening curly quotes and \u201c is the closing curly quotes.

about 4 years ago · Juan Pablo Isaza Relatório

0

You may use this code with 2 .replace method calls:

  1. First we match pair of straight quotes and replace with curly double quotes (html entities).
  2. In second .replace we just match single " and replace with left curly double quote (html entities).

This assumes that single " after quoted pair replacement are opening ones.

Code:

const text = `"This has an opening and a closing quote."

"This only has an opening quotes

This doesn't have quotes.`;

const result = text.replace(/"([^"\n\r]*)"/g, '“$1”').replace(/"/g, '“');

console.log(result)

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