Tell us what’s happening:
How may I get replaced only the first consonant or first consonants group in a string, save it and set in the end of a string.
Your code so far
function translatePigLatin(str) {
let consonantRegex = /[bcdfghjklmnpqrstvwxys]/gi
let replacedConsonants=str.replace(consonantRegex, '')
console.log(replacedConsonants)
}
translatePigLatin("consonant");
Your browser information:
User Agent is: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/81.0.4044.122 Safari/537.36
.
Challenge: Pig Latin
Link to the challenge:
Learn to code. Build projects. Earn certifications.Since 2015, 40,000 graduates have gotten jobs at tech companies including Google, Apple, Amazon, and Microsoft.
ilenia
April 24, 2020, 5:10pm
#2
if you use the g
flag the replace method would replace everything, if you don’t use it only first occurrence will be replaced
you could take a look at documentation, like at the part Switching words in strings
1 Like
I have captured c, but I want to set it at the end of osonant plus “ay”.
function translatePigLatin(str) {
let regEx=/([bcd-fgh-klmn-pqrst-vwxyz])/i
console.log(str.replace(regEx, '$1,'))
}
translatePigLatin("consonant");
I just need to finish 2 tests:
translatePigLatin("algorithm")
should return “algorithmway”.
translatePigLatin("eight")
should return “eightway”.
May I do this with regex or is it neccesary build a new condition?
function translatePigLatin(str) {
let regex=/(.[^aeiou]*)([aeiou]*)(.*)|([aeiou]*)(.^[aeiou]*)(.*)/i
let replacedStr=str.replace(regex, "$2$3$1ay");
console.log(replacedStr)
return replacedStr;
}
translatePigLatin("algorithm");
//let regex=/(.[^aeiou]*)([aeiou]*)(.*)/i;
//let replacedStr=str.replace(regex, "$2$3$1ay");
It is done!
function translatePigLatin(str) {
let regexConsonants=/(.[^aeiou]*)([aeiou]*)(.*)|([aeiou]*)(.[^aeiou]*)(.+)/i
let replacedConsonantStr=str.replace(regexConsonants, "$2$3$1ay");
let replacedVowelsStr=str.replace(/(.+)/i,"$1way")
//console.log(replacedConsonantStr)
if(str.startsWith('a')|str.startsWith('e')|str.startsWith('i')|str.startsWith('o')|str.startsWith('u')){
return replacedVowelsStr;
}else{
return replacedConsonantStr;
}
}
translatePigLatin("algorithm");
//let regex=/(.[^aeiou]*)([aeiou]*)(.*)/i;
//let replacedStr=str.replace(regex, "$2$3$1ay");