英文:
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 = 'čǧǩőšűžČǦǨŐŠŰŽ'
const ccc = wontParse(aa)
console.log(ccc)
}
var wontParse = (function () {
let in_chrs = 'čǧǩőšűžČǦǨŐŠŰŽ',
out_chrs = 'cgkosuzCGKOSUZ',
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 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 = 'čǧǩőšűžČǦǨŐŠŰŽ',
y = 'cgkosuzCGKOSUZ';
let aa = 'čǧǩőšűžČǦǨŐŠŰŽ'
const ccc = wontParse(aa,x,y)
console.log(ccc)
}
I got this to work
var wontParse2 = (in_chrs,out_chrs) => (function () {
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 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) => {
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);
};
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论