javascript 中的 replaceAll 函数不会替换所有出现的地方
replaceAll function in javscript doesn't replace all occurrences
我有以下片段:
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);
此代码段应与 paragraph
中的所有 \t
相匹配,引号之间的除外,并将它们全部替换为逗号 ,
。但是,所有这些都没有被替换,因为我解析的文本如下所示:
'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 '
在 (female)"
之后,\t
根本没有被替换,但它应该替换第一行和第二行中的 \t
。我做错了什么,有什么想法吗?
您可以使用
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);
/("[^"]*")|\t/g
正则表达式匹配所有出现在双引号之间的字符串,将它们捕获到其他上下文中的第 1 组或 TAB 字符中。
如果第 1 组匹配,则替换为第 1 组值,否则,替换为逗号。
我有以下片段:
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);
此代码段应与 paragraph
中的所有 \t
相匹配,引号之间的除外,并将它们全部替换为逗号 ,
。但是,所有这些都没有被替换,因为我解析的文本如下所示:
'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 '
在 (female)"
之后,\t
根本没有被替换,但它应该替换第一行和第二行中的 \t
。我做错了什么,有什么想法吗?
您可以使用
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);
/("[^"]*")|\t/g
正则表达式匹配所有出现在双引号之间的字符串,将它们捕获到其他上下文中的第 1 组或 TAB 字符中。
如果第 1 组匹配,则替换为第 1 组值,否则,替换为逗号。