Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/arrays/14.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_Arrays_For Loop_Replace - Fatal编程技术网

Javascript 如何用下划线替换数组中的某些字母

Javascript 如何用下划线替换数组中的某些字母,javascript,arrays,for-loop,replace,Javascript,Arrays,For Loop,Replace,我正在努力使文本显示在输入框下,其中一些字母应替换为下划线。此文本将是用户输入以获得正确输出的答案 我原以为循环会用\u替换像e这样的字母,但它什么也没做。没有错误消息,只是没有替换字母e。我试图为循环设置一个,我在其他地方找到了这个循环,用于替换数组元素中的某些字符,但没有任何效果 以下是JS代码: let array = ['blue', 'green', 'red', 'orange', 'pink', 'silver', 'purple', 'lime'] let arraySecond

我正在努力使文本显示在输入框下,其中一些字母应替换为下划线。此文本将是用户输入以获得正确输出的答案

我原以为循环会用
\u
替换像e这样的字母,但它什么也没做。没有错误消息,只是没有替换字母e。我试图为循环设置一个
,我在其他地方找到了这个循环,用于替换数组元素中的某些字符,但没有任何效果

以下是JS代码:

let array = ['blue', 'green', 'red', 'orange', 'pink', 'silver', 'purple', 'lime']
let arraySecond = ['blue', 'green', 'red', 'orange', 'pink', 'silver', 'purple', 'lime']; 

//The other array is the one that shows the element, 
//that matches the first array, but with "_", instead of certain letters

document.getElementById('check').addEventListener('click', thisFunction);

function thisFunction() {
   let value = document.getElementById('input').value;
   
   if (value == theColor) {
    alert(`Congrats you gussed right, its ${value}`)
        } else {
    alert(`Oof, your guess was incorrect, it\'s ${array[randomizedArray]}`)
    }
}

let randomizedArray = Math.floor(Math.random() * array.length);

let theColor = array[randomizedArray]; 

//One thing the code above is used for is be in the for loop, to replace letters

for (let i = 0; i < theColor.length; i++) {
    theColor[i] = theColor[i].replace('e', '_');
} 

//This loop that should do the replacing, but it doesn't work

document.getElementById('output').innerHTML = theColor;
let数组=[“蓝色”、“绿色”、“红色”、“橙色”、“粉色”、“银色”、“紫色”、“莱姆色”]
让arraySecond=[‘蓝色’、‘绿色’、‘红色’、‘橙色’、‘粉色’、‘银色’、‘紫色’、‘酸橙’];
//另一个数组是显示元素的数组,
//它与第一个数组匹配,但使用“389;”而不是某些字母
document.getElementById('check')。addEventListener('click',this函数);
函数thisFunction(){
让value=document.getElementById('input').value;
如果(值==颜色){
警报(`恭喜你说对了,它的${value}`)
}否则{
警报(`Oof,您的猜测不正确,它是${array[randomizedArray]}`)
}
}
让randomizedaray=Math.floor(Math.random()*array.length);
让颜色=数组[randomizedArray];
//上面的代码的一个用途是在for循环中替换字母
for(设i=0;i
您正在对单词的单个字母使用
replace
功能,而不是对整个单词使用该功能-您不需要遍历每个字母来替换它们

根据您的具体需要,您可以按如下方式执行(以
e
为例):

  • 替换字母的第一个实例:
    颜色。替换('e','u')
  • 替换字母的所有实例:
    theColor.Replace(/e/g,'.')-请注意,
    /
    ..
    /g
    表示执行全局搜索和替换
  • 不区分大小写的replace:将
    /gi
    添加到上述
    颜色中。replace(/e/gi,'.'''.')
然而,我的猜测是,您将需要替换多个特定字母,因此您可以一次性替换多个字母(例如a、e、i、o和u),如下所示:

  • 替换多个字母的所有实例
    theColor.Replace(/aeiou/gi,'.')
    -是否将
    /
    /gi
    之间的所有字母都替换为
    \uu
示例:

var theColor=“绿色”;
log(“在“+theColor+”中替换e的第一个实例:”
+颜色。替换('e','u');
var theColor=“绿色”;
log(“替换“+theColor+”中的所有e实例(不区分大小写):”
+颜色。替换(/e/gi,'.');
var theColor=“橙色”;
log(“一次替换所有元音”+颜色+:”

+颜色。替换(/[aeiou]/gi,'.')
你能解释一下你想做什么吗,例如,你想用
替换字母
e
(例如)的所有实例,还是能够决定替换哪些实例?这样程序就会选择数组中的某个元素。让我们说蓝色,因为它是数组中的一个元素。然后,它将用下划线替换e,下划线应显示为蓝色。这部分是用第二个数组完成的,第一个数组是用户输入的。好的,我知道了,但是如果单词是“绿色”怎么办?它应该替换它找到的所有
e
s还是只替换一个?是的,像这样谢谢你的帮助和解释@很高兴能帮忙!我看到了一个关于传递到replace函数中的内容的注释通知,但它似乎已被删除。我假设您找到了答案,但如果没有,则
replace
函数可以采用正则表达式模式,例如
/abc/g
-您可以在答案()中链接的文档中找到有关
replace
函数的更多信息,该页面还包括指向RegExp的链接,以获取有关正则表达式模式的更多信息。