【发布时间】:2020-09-21 19:23:55
【问题描述】:
我的任务是:
取一个字符串
将每个元音打印在新行上(按顺序)然后...
在新行上打印每个辅音(按顺序)
我发现的问题与正则表达式有关。我原来用的……
/[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");
【问题讨论】:
-
你想要
.test函数,它返回真或假
标签: javascript regex