replaceAll function in javscript doesn’t replace all occurrences

I have the following snippet:

const paragraph = '1t1t150t18t"Pack of 12 action figures (variety)"t18t9t5.50t2013-01-02 00:00:00.0000000tTruet6t2013-01-02 07:00:00.0000000rn2t1t151t21t"Pack of 12 action figures (male)"t21t9t5.50t2013-01-02 00:00:00.0000000tTruet6t2013-01-02 07:00:00.0000000rn3t1t152t18t"Pack of 12 action figures (female)"t18t9t5.50t2013-01-02 00:00:00.0000000tTruet6t2013-01-02 07:00:00.0000000rn4t2t76t8t"\"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?

Answer

You can use

const paragraph = '1t1t150t18t"Pack of 12 action figures (variety)"t18t9t5.50t2013-01-02 00:00:00.0000000tTruet6t2013-01-02 07:00:00.0000000rn2t1t151t21t"Pack of 12 action figures (male)"t21t9t5.50t2013-01-02 00:00:00.0000000tTruet6t2013-01-02 07:00:00.0000000rn3t1t152t18t"Pack of 12 action figures (female)"t18t9t5.50t2013-01-02 00:00:00.0000000tTruet6t2013-01-02 07:00:00.0000000rn4t2t76t8t"\"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.