我在文件中有以下文字。
10 - Black
10 - Black
10 - Black
10 - Black
10 - Black
20 - Grey/Black
20 - Grey/Black
20 - Grey/Black
20 - Grey/Black
20 - Grey/Black
30 - Blue
30 - Blue
30 - Blue
30 - Blue
30 - Blue
40 - Brown/Red
40 - Brown/Red
40 - Brown/Red
40 - Brown/Red
40 - Brown/Red
这里我想替换下面的副本,但保留下面的字母。
简单地说我希望输出如下所示。
10 - Black
20 - Grey/Black
30 - Blue
40 - Brown/Red
这是我的正则表达式代码:(\b[^\n,]+)\n(?=.*\b\1\b)
regex101:https://regex101.com/r/wF0aG1/1
答案 0 :(得分:1)
为什么要使用正则表达式,何时可以使用简单的JS?
使用jQuery读取文件后,将文件中的数据转换为数组,并从数组中获取唯一数据。
JS
$(document).load('delete.txt', function(data){
temp = data.split('\n');
uniqueArray = temp.filter(function (item, pos) {
return temp.indexOf(item) == pos;
});
console.log(uniqueArray);
});
控制台中的结果是
["10 - Black\r", "20 - Grey/Black\r", "30 - Blue\r", "40 - Brown/Red\r", "40 - Brown/Red"]
答案 1 :(得分:0)