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

127
Views
replaceAll function in javscript doesn't replace all occurrences

I have the following snippet:

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);

This snippet should match all of \t, except the ones between quotes, from paragraph and replace them all with comma ,. However, all of them don't get replaced as the text I parse looks like this:

'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 '

After (female)", the \t doesn't get replaced at all but it should as it replaces \t in first and second row. What am I doing wrong, any ideas?

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

0

You can use

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);

The /("[^"]*")|\t/g regex matches all occurrences of strings between double quotation marks capturing them into Group 1 or TAB char in other contexts.

If Group 1 matches, the replacement is Group 1 value, else, the replacement is a comma.

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!