gpt4 book ai didi

javascript - 检查字符串字符是否与数组值匹配

转载 作者:行者123 更新时间:2023-12-03 07:31:45 24 4
gpt4 key购买 nike

我在完成一个小挑战时遇到了一些麻烦。我试图检查是否在数组中找到了字符串的字符,如果是,则停止循环,记录值,然后从新的字符串字符开始。谁能帮忙

function LetterChanges(str) {    
var alphabet = ["a","b","c","d","e","f","g","h","i","j","k","l","m","n","o","p","q","r","s","t","u","v","w","x","y","z"];
// code goes here
var myString = "";
for(var i = 0; i <= myString.length; i++) {
for(var o = 0; o < 25; o++){
var getChar = myString += str.charAt(i)
if (getChar == alphabet[o]){
alert(getChar);
break;
}
}
}
alert(getChar);
}

// keep this function call here
LetterChanges("Test");`

最佳答案

function LetterChanges(str) {
// code goes here
var regex=/[abcdefghijklmnopqrstuvwxyz]/;

// simply you can give as below
regex = /[a-z]/;

//if you want to match Cap A-Z too make the regex to ignore case as below
// regex = /[a-z]/i;

var myString = "";
for (var i = 0; i < str.length; i++) {
var char = str[i];
if (regex.test(char)) {
myString += char;
console.log(myString)
}
}
console.log(myString);
}

// keep this function call here
LetterChanges("Test");

关于javascript - 检查字符串字符是否与数组值匹配,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35787452/

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