效果:
案例:
strA = '點擊圖標下載 App'
strB = '點擊App圖標下載應(yīng)用'
結(jié)果:
/*
strA變成strB
0:不變的字段慎框;
1:新增的字段
-1:刪除的字段
*/
0: [0, "點擊"]
1: [1, "App"]
2: [0, "圖標下載"]
3: [-1, " App"]
4: [1, "應(yīng)用"]
主要實現(xiàn):
使用第三方庫‘diff_match_patch’, 有各種語言的實現(xiàn)版本,
地址:
https://github.com/google/diff-match-patch
api:
https://github.com/google/diff-match-patch/wiki/API
JS版本的詳細實現(xiàn):
安裝:
npm install diff_match_patch
使用:
import DiffMatchPatch from 'diff-match-patch';
let str1 = '點擊圖標下載 App';
let str2 = '點擊App圖標下載應(yīng)用';
let dmp = new DiffMatchPatch();
let diffs = dmp.diff_main(str1, str2);
console.log(diffs) //[0: [0, "點擊"]
//1: [1, "App"]
//2: [0, "圖標下載"]
//3: [-1, " App"]
//4: [1, "應(yīng)用"]]