Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/string/5.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/sql/72.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何在Javascript中将字符串替换存储为对象?_Javascript_String_Replace - Fatal编程技术网

如何在Javascript中将字符串替换存储为对象?

如何在Javascript中将字符串替换存储为对象?,javascript,string,replace,Javascript,String,Replace,我有以下几点 astring.replace(/(someregex/g, "replacementstring.$1") 我想将它存储为某种对象,以便将来调用它。显而易见的解决方案是将其存储为两个变量someregex和replacementstring,并在将来将其称为astring.replace(someregex,replacementstring+'.$1'),但这对我来说似乎非常笨拙 是否有更简洁的方式存储字符串替换?我目前的想法是将.replace(/someregex/g,“

我有以下几点

astring.replace(/(someregex/g, "replacementstring.$1")
我想将它存储为某种对象,以便将来调用它。显而易见的解决方案是将其存储为两个变量
someregex
replacementstring
,并在将来将其称为
astring.replace(someregex,replacementstring+'.$1')
,但这对我来说似乎非常笨拙


是否有更简洁的方式存储字符串替换?我目前的想法是将
.replace(/someregex/g,“replacementstring.$1”)
存储为名为
stringreplacement
的字符串,并使用
eval(/astring'+stringreplacement)
。但这似乎很愚蠢。

您可以使用currying来定义一个函数,其中一些变量已经预定义

例如:

函数替换(查找、替换){
返回函数(字符串){
返回字符串.replace(查找,替换);
}
}
const replacer=replace(/hello/gi,“G'day”);

log(replacer('hello fubar'))嗯,对于regex部分,您可以将其存储为实际的RegExp对象<代码>风险模式=新的正则表达式(“someregex”,“g”)