打字稿-正则表达式检查字符串是否包含元音

时间:2018-06-19 13:53:12

标签: angular typescript

谁能帮我写一个正则表达式来查看字符串中是否包含元音。

EX : Hi Team // True
     H // False

我正在使用下面的正则表达式,但未得到所需的结果。

[aeiou]

2 个答案:

答案 0 :(得分:2)

检查元音的示例

const withVowels = 'This is a sentence with vowels';
const withoutVowels = 'dfjgbvcnr tkhlgj bdhs'; // I seriously didn't bother there

const hasVowelsRegex = /[aeiouy]/g; 

console.log(!!withVowels.match(hasVowelsRegex));
console.log(!!withoutVowels.match(hasVowelsRegex));

答案 1 :(得分:0)

检查一下。

const regex = /^[aeiouy]+$/gmi;
const str = `aeyiuo
aeYYuo
qrcbk
aeeeee
normal
Text
extTT`;
let m;

while ((m = regex.exec(str)) !== null) {
    // This is necessary to avoid infinite loops with zero-width matches
    if (m.index === regex.lastIndex) {
        regex.lastIndex++;
    }
    
    // The result can be accessed through the `m`-variable.
    m.forEach((match, groupIndex) => {
        console.log(`Found match, group ${groupIndex}: ${match}`);
    });
}