代码之家  ›  专栏  ›  技术社区  ›  Mansoor Ahmed

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

  •  0
  • Mansoor Ahmed  · 技术社区  · 7 年前

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

    EX : Hi Team // True
         H // False
    

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

    [aeiou]
    
    2 回复  |  直到 7 年前
        1
  •  2
  •   Maryannah    7 年前

    检查元音的示例

    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));
        2
  •  0
  •   Alok G.    7 年前

    看看这个。

    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}`);
        });
    }
    推荐文章