为了账号安全,请及时绑定邮箱和手机立即绑定

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

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

侃侃尔雅 2023-09-21 17:18:11
我需要在分隔字符串中,仅当子字符串与替换值匹配时才替换分隔符之间的子字符串在 Google 表格中使用它配套的var ifind = ["AA", "CaaL"];var ireplace = ["zz", "Bob"];zz replaces AABob 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])];andreturn [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])];  }}谢谢
查看完整描述

2 回答

?
慕标5832272

TA贡献1966条经验 获得超4个赞

你的固定功能可以是这样的


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 regex = new RegExp("(?<![^|])(?:" + ifind.join("|") + ")(?![^|])", "g");

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


  function fr(input) {

    return input.map(c => c.replace(regex, (x) => map[x]));

  }

}

图案看起来像


/(?<![^|])(?:AA|CaaL)(?![^|])/g

在哪里

  • (?<![^|])|- 是一个负向后查找,如果紧邻当前位置左侧的字符串有 或 开头,则匹配失败

  • (?:AA|CaaL)- 一个AACaaL

  • (?![^|])|- 是负向前瞻,如果当前位置右侧紧邻字符串的 或 结尾,则匹配失败。

请参阅在线正则表达式演示(因为该演示是针对单个多行字符串而不是一组单独的字符串运行,所以负向查找被替换为正向查找)。


查看完整回答
反对 回复 2023-09-21
?
三国纷争

TA贡献1804条经验 获得超7个赞

要替换的字符串

  • |或 字符串开头^

  • |或 字符串结尾$

正则表达式需要是

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();


查看完整回答
反对 回复 2023-09-21
  • 2 回答
  • 0 关注
  • 64 浏览
慕课专栏
更多

添加回答

举报

0/150
提交
取消
意见反馈 帮助中心 APP下载
官方微信