5 回答

TA贡献1853条经验 获得超6个赞
目前还不完全清楚你是只想要元音字母还是想要除元音字母以外的所有字母。无论哪种方式,一个简单的正则表达式都可以得到你需要的字符。
let str = "Hello World";
let res = str.match(/[aeiou]/ig).join("");
console.log(res);
let res2 = str.match(/[^aeiou]/ig).join("");
console.log(res2);
如果你想要一个数组,请删除该.join("")部分,否则这会给你一个字符串

TA贡献2021条经验 获得超8个赞
您可以循环字符串并将这些元音存储在数组中。
var arr = [];
for(var i = 0; i < str.length; i++){
if(str[i] == 'e' || str[i] == 'o'){
arr.push(str[i]);
}
}
console.log(arr);}

TA贡献1772条经验 获得超5个赞
怎么样:
var str = "Hello world!";
var theGoods = str.split('').filter(c => ['e', 'o'].includes(c)).join('');
或者,如果您想要“反向”行为
var str = "Hello world!";
var theGoods = str.split('').filter(c => !['e', 'o'].includes(c)).join('');

TA贡献1886条经验 获得超2个赞
提取它们非常容易,只要您知道 RegEx(正则表达式)
var str = "Hello world!" // The original string
var res = str.match(/[aeiou]/gi).join("") // Extracting the vowels
// If you want to get the consonants, here you go.
var res2 = str.match(/[^aeiou]/gi).join("")
// Logging them both
console.log(res)
console.log(res2)

TA贡献1877条经验 获得超1个赞
function deletevowels(str) {
let result = str.replace(/[aeiou]/g, '')
return result
}
var text = "Hi test of Replace Javascript";
const a = deletevowels(text);
console.log(a);
添加回答
举报