Home > Net >  How to match a vowel in the beginning of word Regex
How to match a vowel in the beginning of word Regex

Time:09-24

function translatePigLatin(str) {
    let regExp = /^[aieou]/;
    let output = str.match(regExp);
    return output;
}

console.log(translatePigLatin("california"));

I want to match a vowel in the beginning of the word implementing regex. I search but I don't find a serious result. I want to search on word if begin with consonnant or consonant cluster, and return true. Else, return false.

My code:

CodePudding user response:

You just have to negate the result to achieve the result

function translatePigLatin(str) {
  let regExp = /^[aieou]/;
  let output = str.match(regExp);
  return !output;
}

console.log(translatePigLatin("california"));
console.log(translatePigLatin("alifornia"));

You can easily achieve return the boolean value using RegExp

function translatePigLatin(str) {
  return !new RegExp("^[aeiou]").test(str);
}

console.log(translatePigLatin("california"));
console.log(translatePigLatin("alifornia"));

  • Related