首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >我是如何一次运行替换函数的?

我是如何一次运行替换函数的?
EN

Stack Overflow用户
提问于 2019-09-10 08:49:19
回答 2查看 153关注 0票数 0

我希望replaceFunction只运行一次。目前,E-1只在第一次正确工作时才返回Ε-1 (APPLE),但是当用户再次尝试编辑文本字段时,系统会检测到Ε-1并返回Ε-1 (APPLE) (APPLE)

代码语言:javascript
复制
td.onchange = function(e) {
    this.value = this.value.replace(/(\E-(\d+))/g, replaceFunction);

    function replaceFunction(match) {
        // add additional rules here for more cases
        if (match === "E-1") return "Ε-1 (APPLE)";
        if (match === "E-2") return "Ε-2 (SUMSUNG)";
        .
        .
        .
        if(match === "E-99") return "Ε-99 (LG)";
        return match;
    }
}

我怎么阻止这一切?

EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2019-09-10 08:57:05

您可以使用类似的另一个条件:

代码语言:javascript
复制
if (match === "E-1" && match !== "Ε-1 (APPLE)") return "Ε-1 (APPLE)";

如果将映射放入对象中,则可以对此进行优化:

代码语言:javascript
复制
var map = {
    "E-1": "Ε-1 (APPLE)",
    ...
}

if (map[match] && !map[match] !== match) { return map[match]; }

要使其有效,您需要正则表达式,该正则表达式也与括号中的单词后面的单词匹配:

代码语言:javascript
复制
var names = ['APPLE', 'SAMSUNG'];

var re = new RegExp('(E-(\\d+))(?! \\((?:' + names.join('|') + ')\\))', 'g');

另一种解决方案是只使用数组(只有当您在数组中使用E匹配索引时才能使用)

代码语言:javascript
复制
var names = ['APPLE', 'SAMSUNG'];
var re = new RegExp('(E-(\\d+))(?! \\((?:' + names.join('|') + ')\\))', 'g');
// regex explanation, same as yours but \\d is because it's a string
// we create negative look ahead so we check if next text
// after E-1 is not " (" and any of the names.
// we use (?: to group what's inside it's the same as with ()
// but the value will not be captured so there will be
// no param in function for this group
// so this regex will be the same as yours but will not match "E-1 (APPLE)"
// only "E-1"

this.value = this.value.replace(re, replaceFunction);

function replaceFunction(match, eg, num) {
   // convert string to number E starts 
   var i = parseInt(num, 10) - 1;
   if (i <= names.length) {
     return match + ' (' + names[i] + ')';
   }
}

regex和函数可以在change函数之外创建,因此它不会在每次更改时创建新函数。

票数 1
EN

Stack Overflow用户

发布于 2019-09-10 09:18:36

替换时,也可以选择向前看后面的空格和括号。这样,在替换器函数中,您可以检查下面的值是否已经是您想要的值(例如,(APPLE))。如果是,那么什么也不做-否则,用新字符串替换:

代码语言:javascript
复制
const replacementsE = [
  , // nothing for E-0
  'APPLE',
  'SUMSUNG',
];

td.onchange = function(e) {
  td.value = td.value.replace(/E-(\d+)(?= \(([^)]+)\)|)/g, replaceFunction);

  function replaceFunction(match, digits, followingString) {
    const replacement = replacementsE[digits];
    if (!replacement || replacement === followingString) {
      return match;
    }
    return `E-${digits} (${replacement})`;
  }
}
代码语言:javascript
复制
<input id="td">

/E-(\d+)(?= \(([^)]+)\)|)/所做的是:

  • E- - Match E-
  • (\d+) -捕获组中的数字
  • (?= \(([^)]+)\)|)向前看任一种:
    • \(([^)]+)\):文字(,后面是非)字符,后面是).如果匹配,非)字符将是第二个捕获组。
    • | -或匹配空字符串(以便查找工作)

这些数字将是第一个捕获组;回调中的digits变量。非)字符将是第二个捕获组;回调中的followingString变量。

如果还希望删除最终的),那么将最终的)设置为可选的,并确保字符集与空格不匹配(这样,没有结束)APPLE后面的空格就不会匹配):

代码语言:javascript
复制
const replacementsE = [
  , // nothing for E-0
  'APPLE',
  'SUMSUNG',
];

td.onchange = function(e) {
  td.value = td.value.replace(/E-(\d+)(?= \(([^) ]+)\)?|)/g, replaceFunction);

  function replaceFunction(match, digits, followingString) {
    const replacement = replacementsE[digits];
    if (!replacement || replacement === followingString) {
      return match;
    }
    console.log(followingString)
    return `E-${digits} (${replacement})`;
  }
}
代码语言:javascript
复制
<input id="td">

如果希望删除最终)之前的任意数量的字符,请检查替换的startsWith是否为以下字符串:

代码语言:javascript
复制
const replacementsE = [
  , // nothing for E-0
  'APPLE',
  'SUMSUNG',
];

td.onchange = function(e) {
  td.value = td.value.replace(/E-(\d+)(?= \(([^) ]+)\)?|)/g, replaceFunction);

  function replaceFunction(match, digits, followingString, possibleTrailingParentheses) {
    const replacement = replacementsE[digits];
    if (!replacement || replacement === followingString || replacement.startsWith(followingString)) {
      return match;
    }
    
    return `E-${digits} (${replacement})`;
  }
}
代码语言:javascript
复制
<input id="td">

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/57867178

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档