Home > Software engineering >  replaceAll function in javscript doesn't replace all occurrences
replaceAll function in javscript doesn't replace all occurrences

Time:11-01

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?

CodePudding user response:

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);
<iframe name="sif1" sandbox="allow-forms allow-modals allow-scripts" frameborder="0"></iframe>

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.

  • Related