gpt4 book ai didi

javascript - 如何替换变量中的更多字符串?

转载 作者:行者123 更新时间:2023-11-28 11:13:45 24 4
gpt4 key购买 nike

在 PHP 中这很容易:

str_replace (array(...), array(...), $subject)

但是如何在 JavaScript 中做到这一点呢?

最佳答案

尝试以下操作:

​var a = ["one", "two", "three"];
var b = ["alpha", "beta", "gamma"];
var stringa = "one to two to three";

然后:​​​​

for (i in a) {
if (i < b.length) {
stringa = stringa.replace(a[i], b[i]);
}
}
console.log(stringa);

关于javascript - 如何替换变量中的更多字符串?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9789598/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com