gpt4 book ai didi

javascript - 如何用不同的单词替换字符串中的多个相同单词

转载 作者:行者123 更新时间:2023-12-01 04:07:59 24 4
gpt4 key购买 nike

我想用不同的单词替换字符串中的多个相同单词。

例如,

"What is your %s name and %s name"

我想将第一个 '%s' 替换为 'first',将第二个 '%s' 替换为 'last' .

最佳答案

我认为这可能更干净:

['first', 'last'].forEach(function(tag) { x = x.replace('%s', tag); })

其中 x 是您要替换的字符串。

关于javascript - 如何用不同的单词替换字符串中的多个相同单词,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41662306/

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