在分隔字符串中,仅当子字符串与替换值匹配时才替换分隔符之间的子字符串

我需要在分隔字符串中,仅当子字符串与替换值匹配时才替换分隔符之间的子字符串


在 Google 表格中使用它


配套的


var ifind = ["AA", "CaaL"];

var ireplace = ["zz", "Bob"];


zz replaces AA

Bob replaces CaaL

我有


| Id | Segment            |

|----|--------------------|

| 1  | AAA AA|AA|CaaL AA  |

| 2  | AAA-AA|AA|CaaL     |

| 3  | AAA, AA|AA|AA      |

| 4  | AA                 |

| 5  | AA AA              |

| 6  | AA, AA             |

| 7  |                    |

| 8  | CaaL               |

| 9  | AA                 |

我需要


| Id | Segment           |

|----|-------------------|

| 1  | AAA AA|zz|CaaL AA |

| 2  | AAA-AA|zz|Bob     |

| 3  | AAA, AA|zz|zz     |

| 4  | zz                |

| 5  | AA AA             |

| 6  | AA, AA            |

| 7  |                   |

| 8  | Bob               |

| 9  | zz                |

我已经尝试过(除其他外)


return [iFinds.reduce((str, k) => str.replace(new RegExp('\\b'+k+'\\b','g'),map[k]), input[0])];


and


 return [iFinds.reduce((str, k) => str.replace(new RegExp('(?![ .,]|^)?(\\b' + k + '\\b)(?=[., ]|$)','g'),map[k]), input[0])];


and


return [iFinds.reduce((str, k) => str.split(k).join (map[k]), input[0])];

我的功能(来自这里)


function findReplace_mod1() {

  const ss = SpreadsheetApp.getActiveSheet();

  const col = ss.getRange(2, 3, ss.getLastRow()).getValues();

  

  var ifind = ["AA", "Caal"];

  var ireplace = ["zz", "Bob"];

  

  var map = {};

  ifind.forEach(function(item, i) {

    map[item] = ireplace[i];

  });

  

  //const map = { Fellow: 'AAA', bob: 'BBB' };

  const iFinds = Object.keys(map);

  ss.getRange(2, 3, ss.getLastRow()).setValues(col.map(fr));


  function fr(input) {

    return [iFinds.reduce((str, k) => str.replace(k, map[k]), input[0])];

  }

}


谢谢


侃侃尔雅
浏览 120回答 2
2回答

慕标5832272

你的固定功能可以是这样的function findReplace_mod1() {&nbsp; const ss = SpreadsheetApp.getActiveSheet();&nbsp; const col = ss.getRange(2, 3, ss.getLastRow()).getValues();&nbsp; var ifind = ["AA", "CaaL"];&nbsp; var ireplace = ["zz", "Bob"];&nbsp;&nbsp;&nbsp; var map = {};&nbsp; ifind.forEach(function(item, i) {&nbsp; &nbsp; map[item] = ireplace[i];&nbsp; });&nbsp;&nbsp;&nbsp; //const map = { Fellow: 'AAA', bob: 'BBB' };&nbsp; const regex = new RegExp("(?<![^|])(?:" + ifind.join("|") + ")(?![^|])", "g");&nbsp; ss.getRange(2, 3, ss.getLastRow()).setValues(col.map(fr));&nbsp; function fr(input) {&nbsp; &nbsp; return input.map(c => c.replace(regex, (x) => map[x]));&nbsp; }}图案看起来像/(?<![^|])(?:AA|CaaL)(?![^|])/g在哪里(?<![^|])|- 是一个负向后查找,如果紧邻当前位置左侧的字符串有 或 开头,则匹配失败(?:AA|CaaL)- 一个AA或CaaL(?![^|])|- 是负向前瞻,如果当前位置右侧紧邻字符串的 或 结尾,则匹配失败。请参阅在线正则表达式演示(因为该演示是针对单个多行字符串而不是一组单独的字符串运行,所以负向查找被替换为正向查找)。

三国纷争

要替换的字符串以|或 字符串开头^以|或 字符串结尾$正则表达式需要是str.replace(new Regexp(`(\\||^)${k}(\\||$)`), `$1${map[k]}$2`)/*<ignore>*/console.config({maximize:true,timeStamps:false,autoScroll:false});/*</ignore>*/function findReplace_mod2() {  const col = [['AAA AA|AA|CaaL AA'], ['AA'], ['AAA-AA|AA|CaaL']];  const map = { AA: 'zz', CaaL: 'Bob' };  const iFinds = Object.keys(map);  console.info(col.map(fr));  function fr(input) {    return [iFinds.reduce((str, k) => str.replace(new RegExp(`(\\||^)${k}(\\||$)`), `$1${map[k]}$2`), input[0])];  }}findReplace_mod2();
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

JavaScript