有没有办法在javascript中做到这一点?
if('a' in array[index] or 'A' in array[index] ):
print(bArray[0], end = ' ')
(在数组中搜索字符串,返回该字符串的索引,然后console.log(bArray[location];
)
有没有办法在javascript中做到这一点?
if('a' in array[index] or 'A' in array[index] ):
print(bArray[0], end = ' ')
(在数组中搜索字符串,返回该字符串的索引,然后console.log(bArray[location];
)
是的
// Convert the string to lower case so that it will match both 'a' and "A"
const aIndex = array[index].toLowerCase().indexOf('a');
if(aIndex !== -1) {
console.log(aIndex);
}
这是通过正则表达式匹配最容易完成的:
let match = s.match(/[a]/i);
if (match) console.log(match.index, match[0]);
如果您想要更通用的版本:
let matcher = s => {
let regex = new RegExp(s, 'i');
let match = s.match(regex);
return match && [match.index, match[0]];
};