Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

131
Vistas
How to make the following regex match and replace unclosed double quotes?

The following code replace straight double quotes with curly double quotes:

 const input = `"Line 1"

"Line 2

"Line 3"

Line 4`
 
const output = input.replace(/\"(.*?)(\")/g, '“$1”')
 
console.log(output)

There's a problem, though. Sometimes a line doesn't have a closing double quote (which indicates that the quotes continues on the line below). So the opening double quote won't be replaced:

`“Line 1$1”

"Line 2

“Line 3$1”

Line 4`

How to modify the regex so it also replaces opening double quotes that aren't followed by a closing double quote?

Desired output:

`“Line 1$1”

“Line 2

“Line 3$1”

Line 4`
about 4 years ago · Juan Pablo Isaza
3 Respuestas
Responde la pregunta

0

I would go in 2 successive replace(), if it's ok:

  • one for caching the "...'" form
  • another for catching the remaining "...
const output = input
    .replace(/\"(.*?)(\")/g, '“$1”')
    .replace(/\"(.*)/g, '“$1”')

But I didn't understand if you wanted to match multilines results in an unique final string, or just add quotes to the end of each line

about 4 years ago · Juan Pablo Isaza Denunciar

0

  1. List item

const input = `"Line 1"

    "Line 2

    "Line 3"

    Line 4`
     
      const output = input.replace(/"([^"/]*)"/g, '“$1”')

     
    console.log(output)

about 4 years ago · Juan Pablo Isaza Denunciar

0

You can use

.replace(/^"([^"\n\r]*)"?$/gm, "“$1”")

See the regex demo. The regex matches all non-overlapping occurrences (g), while matching start of lines with ^ and end of lines with $ (due to m) and means

  • ^ - start of a line
  • " - a double quotation mark
  • ([^"\n\r]*) - Group 1: any zero or more chars other than ", CR and LF
  • "? - an optional " char
  • $ - end of any line.

See the JavaScript demo:

 const input = `"Line 1"

"Line 2

"Line 3"

Line 4`
 
const output = input.replace(/^"([^"\n\r]*)"?$/gm, '“$1”')
 
console.log(output)

about 4 years ago · Juan Pablo Isaza Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda