Python to js word to sponetics 脚本
Python to js word to phonetics script
在 javascript 中有没有办法做到这一点?
if('a' in array[index] or 'A' in array[index] ):
print(bArray[0], end = ' ')
(搜索字符串的数组,return 该字符串的索引,然后 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]];
};
在 javascript 中有没有办法做到这一点?
if('a' in array[index] or 'A' in array[index] ):
print(bArray[0], end = ' ')
(搜索字符串的数组,return 该字符串的索引,然后 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]];
};