如何搜索 4 个不同字符(之一)的字符串并根据结果继续?
How to search string for (one of) 4 different chars and proceed depending on result?
我想在一个字符串中搜索 4 个不同的字符,然后继续使用找到的第一个字符。
尝试将 .lastIndexOf 与 .search 结合使用
输入必须是字符串。
输出必须是最后出现的 4 个常量不同字符之一。
例如:
输入字符串=“我们能做到”
函数查找字符“w”、“e”、“n”、“i”
想要 return = 我
我不知道我是否理解正确,但看看这个。
const a = "my example string";
console.log(lastCharOccurrence(a));
function lastCharOccurrence(text) {
const chars = ["a", "m", "s", "r"];
let last;
for (const char of text) {
if(chars.includes(char)) {
last = char;
}
}
return last;
}
如果没有找到字符,则不会进行检查,因此请确保在需要时添加一个。
我想在一个字符串中搜索 4 个不同的字符,然后继续使用找到的第一个字符。
尝试将 .lastIndexOf 与 .search 结合使用
输入必须是字符串。 输出必须是最后出现的 4 个常量不同字符之一。
例如: 输入字符串=“我们能做到” 函数查找字符“w”、“e”、“n”、“i” 想要 return = 我
我不知道我是否理解正确,但看看这个。
const a = "my example string";
console.log(lastCharOccurrence(a));
function lastCharOccurrence(text) {
const chars = ["a", "m", "s", "r"];
let last;
for (const char of text) {
if(chars.includes(char)) {
last = char;
}
}
return last;
}
如果没有找到字符,则不会进行检查,因此请确保在需要时添加一个。