I am attempting to write a function that will replace all of the vowels in a string with another arbitrary vowel which is selected by an end user. So far, I have been able to write a function that will replace all of the vowels regardless of case, but I would like to be able to preserve the case during replace()
.
Here is an example of what I am doing right now.
var selectedVowel = "a";
var vowels = /[aeiouAEIOU]/gi;
function rep(string){
let newString = string.replace(vowels, selectedVowel);
return newString;
}
rep("FooBar Exe. unIt");
// returns "FaaBar axe. anat"
// Intended output should return "FaaBar Axe. anAt"
I have tried using Regular Expressions to modify the search criteria for replace()
and selectedVowel
, but I can't figure out how to use the right regex characters to achieve that end.
I have also looked into methods that use split()
to replace the first letter of a word, but this method seems to be limited to the string's indexes, which are not known at the time of the function call.
Any suggestions?