【发布时间】:2021-02-20 23:45:29
【问题描述】:
我正在尝试使用正则表达式来评估索引 0 处的 newArray 是否等于存储在 vowel 中的值。我知道这种方法不起作用,但我不明白为什么。
顺便说一句,我刚刚开始学习如何编码,所以我只知道 Vanilla JS
function translate(val) {
let newArray = Array.from(val)
let vowel = /^[aeiouy]/gi
let consonant = /[^aeiouy]/
if (newArray[0] == vowel) {
return 'vowel';
} else if (newArray[0] == consonant) {
return 'consonant'
} {
return 'none';
}
}
translate('inglewood')
【问题讨论】:
-
Array.from(var)应该是Array.from(val)吗? -
是的!刚刚修复了@ThumChoonTat
-
newArray[0] == consonant这不是正则表达式的使用方式...使用 String .match 或 RegExp .test
标签: javascript regex