【发布时间】:2018-11-29 03:04:43
【问题描述】:
我需要用字符串中的索引 + 1 替换字符串中的每个元音。
(函数名 = vowel2index)
输出示例:
vowel2index('this is my string') == 'th3s 6s my str15ng'
我希望这个例子能说明我想要做什么。我尝试使用如下替换方法:
str.replace(/[aeiou]/gi, str.indexOf(/[aeiou]/gi);
但这并没有解决问题。我也试过下面的代码,但我不知道从 IF 语句到哪里去(我还没有使用数组元音):
function vowel2index(str) {
let vowels = ["a", "e", "i", "o", "u"];
let arr = [];
for(i = 0; i < str.length; i++){
if (str[i] === /[aeiou]/gi) {
arr.push(str.replace(str[i], indexOf(str[i])+1));
}
任何帮助表示赞赏。作为一个仅供参考,这个问题来自代码战。
【问题讨论】:
标签: javascript arrays regex loops