Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

130
Views
La función replaceAll en javscript no reemplaza todas las ocurrencias

Tengo el siguiente fragmento:

 const paragraph = '1\t1\t150\t18\t\"Pack of 12 action figures (variety)\"\t18\t9\t5.50\t2013-01-02 00:00:00.0000000\tTrue\t6\t2013-01-02 07:00:00.0000000\r\n2\t1\t151\t21\t\"Pack of 12 action figures (male)\"\t21\t9\t5.50\t2013-01-02 00:00:00.0000000\tTrue\t6\t2013-01-02 07:00:00.0000000\r\n3\t1\t152\t18\t\"Pack of 12 action figures (female)\"\t18\t9\t5.50\t2013-01-02 00:00:00.0000000\tTrue\t6\t2013-01-02 07:00:00.0000000\r\n4\t2\t76\t8\t\"\\\"The '; const regex = /(?!\B"[^"]*)\t(?![^"]*"\B)/g; const found = paragraph.replaceAll(regex, ','); console.log(found);

Este fragmento debe coincidir con todo \t , excepto los que están entre comillas, del paragraph y reemplazarlos todos con coma , . Sin embargo, no se reemplazan todos porque el texto que analizo se ve así:

 '1,1,150,18,"Pack of 12 action figures (variety)",18,9,5.50,2013-01-02 00:00:00.0000000,True,6,2013-01-02 07:00:00.0000000 2,1,151,21,"Pack of 12 action figures (male)",21,9,5.50,2013-01-02 00:00:00.0000000,True,6,2013-01-02 07:00:00.0000000 3,1,152,18,"Pack of 12 action figures (female)" 18 9 5.50 2013-01-02 00:00:00.0000000 True 6 2013-01-02 07:00:00.0000000 4 2 76 8 "\"The '

Después (female)" , \t no se reemplaza en absoluto, pero debería, ya que reemplaza a \t en la primera y segunda fila. ¿Qué estoy haciendo mal? ¿Alguna idea?

about 4 years ago · Juan Pablo Isaza
1 answers
Answer question

0

Puedes usar

 const paragraph = '1\t1\t150\t18\t\"Pack of 12 action figures (variety)\"\t18\t9\t5.50\t2013-01-02 00:00:00.0000000\tTrue\t6\t2013-01-02 07:00:00.0000000\r\n2\t1\t151\t21\t\"Pack of 12 action figures (male)\"\t21\t9\t5.50\t2013-01-02 00:00:00.0000000\tTrue\t6\t2013-01-02 07:00:00.0000000\r\n3\t1\t152\t18\t\"Pack of 12 action figures (female)\"\t18\t9\t5.50\t2013-01-02 00:00:00.0000000\tTrue\t6\t2013-01-02 07:00:00.0000000\r\n4\t2\t76\t8\t\"\\\"The '; const regex = /("[^"]*")|\t/g; const found = paragraph.replace(regex, (whole_match,group_one) => group_one || ',' ); console.log(found);

La expresión regular /("[^"]*")|\t/g coincide con todas las apariciones de cadenas entre comillas dobles y las captura en el grupo 1 o el carácter TAB en otros contextos.

Si el Grupo 1 coincide, el reemplazo es el valor del Grupo 1; de lo contrario, el reemplazo es una coma.

about 4 years ago · Juan Pablo Isaza Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!