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);
添加回答
舉報