将替换函数通用化以接受参数 JavaScript

huangapple go评论72阅读模式
英文:

Generalizing a replace function to accept parameters JavaScript

问题

我要编辑这个函数,以便像这样调用它: const ccc = wontParse(aa, x, y)

我不太了解这个函数,特别是这部分:

transl    = {}, i,
lookup    = function (m) { return transl[m] || m; };

以及最后的 ()

以下是修改后的函数:

function test() {
  let x  = 'čǧǩőšűžČǦǨŐŠŰŽ',
      y  = 'cgkosuzCGKOSUZ';

  let aa = 'čǧǩőšűžČǦǨŐŠŰŽ';

  const ccc = wontParse(aa, x, y);
  console.log(ccc);
}

var wontParse = function (s, in_chrs, out_chrs) {
  let chars_rgx = new RegExp('[' + in_chrs + ']', 'g');
  let transl = {}, i;
  let lookup = function (m) { return transl[m] || m; };

  for (i = 0; i < in_chrs.length; i++) {
    transl[in_chrs[i]] = out_chrs[i];
  }

  return s.replace(chars_rgx, lookup);
}

通过这种方式,你可以像这样调用 wontParse 函数:const ccc = wontParse(aa, x, y),而不需要额外的括号。

这个修改后的函数接受三个参数:s 是要替换的字符串,in_chrs 包含要替换的字符,out_chrs 包含替换后的字符。

英文:

I have the following function.

from here
https://stackoverflow.com/questions/286921/efficiently-replace-all-accented-characters-in-a-string

function test() {
  let aa = &#39;čǧǩőšűžČǦǨŐŠŰŽ&#39;

  const ccc = wontParse(aa)
  console.log(ccc)

}

var wontParse = (function () {
  let in_chrs   = &#39;čǧǩőšűžČǦǨŐŠŰŽ&#39;,
      out_chrs  = &#39;cgkosuzCGKOSUZ&#39;, 
      chars_rgx = new RegExp(&#39;[&#39; + in_chrs + &#39;]&#39;, &#39;g&#39;),
      transl    = {}, i,
      lookup    = function (m) { return transl[m] || m; };

  for (i=0; i&lt;in_chrs.length; i++) {
    transl[ in_chrs[i] ] = out_chrs[i];
  }

  return function (s) { return s.replace(chars_rgx, lookup); }
})();

I want to edit the function, so to accept two additional parameters so as to call it like so:

function test() {
  let x  = &#39;čǧǩőšűžČǦǨŐŠŰŽ&#39;,
      y  = &#39;cgkosuzCGKOSUZ&#39;;

  let aa = &#39;čǧǩőšűžČǦǨŐŠŰŽ&#39;

  const ccc = wontParse(aa,x,y)
  console.log(ccc)

}

I got this to work

var wontParse2 = (in_chrs,out_chrs) =&gt; (function () {
  let chars_rgx = new RegExp(&#39;[&#39; + in_chrs + &#39;]&#39;, &#39;g&#39;),
      transl    = {}, i,
      lookup    = function (m) { return transl[m] || m; };

  for (i=0; i&lt;in_chrs.length; i++) {
    transl[ in_chrs[i] ] = out_chrs[i];
  }

  return function (s) { return s.replace(chars_rgx, lookup); }
})();

So I can call it like this const ccc = wontParse(x,y)(aa)

But how to edit the function so it can be called like this? const ccc = wontParse(aa,x,y)

I do not understand the function well enough to do so
particularly this

transl    = {}, i,
lookup    = function (m) { return transl[m] || m; };

and the last ()

答案1

得分: 1

您只是将s参数移到原始调用中。然后,您可以像以前一样返回s.replace(...)。您还不需要将函数体包装在IIFE(立即调用的函数表达式)中。

var wontParse2 = (s, in_chrs, out_chrs) => {
  let chars_rgx = new RegExp("[" + in_chrs + "]", "g"),
    transl = {},
    i,
    lookup = function (m) {
      return transl[m] || m;
    };

  for (i = 0; i < in_chrs.length; i++) {
    transl[in_chrs[i]] = out_chrs[i];
  }

  return s.replace(chars_rgx, lookup);
};
英文:

You're basically just moving the s parameter into the original call. Then you just return s.replace(...) as you did before. You also don't need to wrap the body in an IIFE.

var wontParse2 = (s, in_chrs, out_chrs) =&gt; {
  let chars_rgx = new RegExp(&quot;[&quot; + in_chrs + &quot;]&quot;, &quot;g&quot;),
    transl = {},
    i,
    lookup = function (m) {
      return transl[m] || m;
    };

  for (i = 0; i &lt; in_chrs.length; i++) {
    transl[in_chrs[i]] = out_chrs[i];
  }

  return s.replace(chars_rgx, lookup);
};

huangapple
  • 本文由 发表于 2023年3月4日 02:57:28
  • 转载请务必保留本文链接:https://go.coder-hub.com/75630894.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定