gpt4 book ai didi

javascript - JS中用数组元素替换var字符串中的单词

转载 作者:行者123 更新时间:2023-11-28 03:40:22 26 4
gpt4 key购买 nike

我的代码自动搜索字符串中的颜色名称并添加随机数后缀并将它们存储为数组中的元素。

我想要的是使用数组中新修改的元素创建一个新字符串。

当字符串多次出现相同的颜色名称时就会出现问题。

我需要的是将这些出现的情况一一替换为导出数组中的不同元素。

(我不想拆分数组中的字符串,将相同的元素替换为全新数组中的另一个数组,然后将其连接到字符串。我需要修改原始字符串)

示例:

字符串通过用户输入更改,所以如果我有:

 str = ' little red fox is blue and red cat is blue';

然后我的代码找到所有颜色名称并生成一个新数组,如下所示:

array = [ 'red2354' , 'blue7856' , 'red324', 'blue23467'] ;

(我的代码在每个颜色元素的末尾添加随机后缀,但我的数组的顺序与字符串的出现顺序相同)

期望的输出:

 str = ' little red2354 fox is blue7856 and red324 cat is blue23467 '; 

到目前为止我尝试过:

var str = ' little red fox is blue and red cat is blue ';  

//I have split string to Array:

ar1 = [ "little","red","fox","is","blue","and","red","cat","is","blue"];

//var dup = matchmine(ar1) find all color duplicates :

var dup = ["red","blue","red","blue"];

//I've sorted all duplicates to appear only once for option B:

var dup2 = ["red","blue"];

//var res = modify(str) takes all color names and adds random suffixes:


var res= ["redA" , "blueA" , "redB", "blueB" ] ;

//I have also create a new array with all strings in case I needed to match lengths:

var final = [ "little","redA","fox","is","blueA","and","redB","cat","is","blueB"];




var i = ar1.length-1;

for ( i ; i >= 0; i--) {

var finalAr = str.replace(ar1[i],final[i]);
str = finalAr;}

alert(finalAr);

问题是循环进行并且第一次将所有元素一一替换。到目前为止一切顺利,但在下面的循环中再次替换第一个。

循环结果:

   str = 'little redAB fox is blueAB and red cat is blue ' 

期望的输出:

 str = 'little redA fox is blueA and redB cat is blueB '

最佳答案

您的一些逻辑仍然隐藏在您的问题中,例如您基于什么理由确定哪个单词应该获得后缀,或者如何确定该后缀。

所以我的回答并不完整。我将假设所有单词都是重复的(包括“is”)。如果您已经知道如何隔离应该考虑的单词,您可以将您的单词选择逻辑注入(inject)到我查找重复项的位置。

对于后缀确定,我提供了一个非常简单的函数,它在每次调用时(按顺序)生成一个唯一的数字。同样,如果您有更合适的逻辑来生成这些后缀,您可以在那里注入(inject)代码。

我建议您根据已识别的单词创建一个正则表达式,然后使用该正则表达式对字符串调用replace,并使用回调参数添加动态后缀。

代码:

function markDupes(str, getUniqueNum) {
// 1. Get all words that are duplicate (replace with your own logic)
let dupes = str.match(/\S+/g).reduce(({words, dupes}, word) => ({
dupes: words.has(word) ? dupes.concat(word) : dupes,
words: words.add(word)
}), {words: new Set, dupes: []} ).dupes;
// Turn those words into a regular expression. Escape special characters:
dupes = dupes.map(word => word.replace(/[\/\\^$*+?.()|[\]{}]/g, '\\$&'))
.join("|");
let regex = new RegExp(dupes, "g");
// Make the replacement of duplicate words and call the callback function:
return str.replace(regex, word => word + getUniqueNum());
}

// Example has two inputs:

// 1. A function that determines the suffix:
// Every call should return a different value
var getUniqueNum = ((i = 1) => {
// ... here we choose to just return an incremental number
// You may implement some other (random?) logic here
return () => i++;
})();

// 2. The input string
let str = 'little red fox is blue and red cat is blue ';

// The call:
console.log(markDupes(str, getUniqueNum));

关于javascript - JS中用数组元素替换var字符串中的单词,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57346388/

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