我的任务如下:
取一个字符串
在新行上打印每个元音(按顺序),然后...
在新行上打印每个辅音(按顺序)
我发现的问题是正则表达式。我最初使用...
/[aeiouAEIOU\s]/g
但这会用元音返回0,用辅音返回-1(所以一切都反过来发生)。我真的很难理解为什么,并且无法为我的生活找到答案。最后,它很简单,只是反转字符串,但我想知道为什么会以这种方式发生。任何人都可以帮忙吗?
let i;
let vowels = /[^aeiouAEIOU\s]/g;
let array = [];
function vowelsAndConsonants(s) {
for(i=0;i<s.length;i++){
//if char is vowel then push to array
if(s[i].search(vowels)){
array.push(s[i]);
}
}
for(i=0;i<s.length;i++){
//if char is cons then push to array
if(!s[i].search(vowels)){
array.push(s[i]);
}
}
for(i=0;i<s.length;i++){
console.log(array[i]);
}
}
vowelsAndConsonants("javascript");
开心每一天1111
白猪掌柜的
莫回无
皈依舞
翻阅古今
随时随地看视频慕课网APP
相关分类