MrPizzaFace
MrPizzaFace

Reputation: 8086

JavaScript replace all the vowels in a string toUpperCase?

Again new to JavaScript and coming over from Ruby/Python so I feel like JS is a smack in the face but yet I'm committed to mastering it. I want to replace all of the vowels to vowels.toUpperCase().

Example 1 I tried with map but I couldn't understand how to pass the index of the current iteration:

function vowelToUpCase(idx){
    if(vowel.test(new_arr[idx])) {
        new_arr[idx] = new_arr[idx].toUpperCase();
    }
}

new_arr.map(vowelToUpCase(idx));
new_arr.join('');


Example 2 Anyway I figured this can be easily done on a str with some simple regex, well I figured wrong because I'm stuck with what you see below.

var new_arr = [ 'e', 'i', 'f', 'm', 'e', 'm', 'p', '*', '3' ];
var vowel = /[aeiou]/gi;
var str = new_arr.join('');
console.log(str);

str.replace(/([aeiou])/gi, function (match) { 
    var str = match.toUpperCase();
    console.log(str);
});
console.log(str);

Output:

eifmemp*3
E
I
E
eifmemp*3

Desired Output:

EIfmEmp*3

Questions:

  1. Can you pass an index to the map() function?
  2. What's wrong with my str.replace example?

Upvotes: 0

Views: 4135

Answers (4)

Vontei
Vontei

Reputation: 1897

Here is another example of a custom filter.

var array = [ 'e', 'i', 'f', 'm', 'e', 'm', 'p', '3','h','e','l','l','o'];
var result = []
array.forEach(function (e) {
  /[aeiouAEIOU]/.test(e) ? '' : result.push(e);
})
console.log(result)

Upvotes: 0

Sam
Sam

Reputation: 20486

Two fundamental problems:

  1. .replace() doesn't replace the string, it returns a new value (so we need to do str = str.replace();

  2. Replace's callback function expects a returned value to successfully replace.

Final code:

str = str.replace(/([aeiou])/g, function (match) { 
    return match.toUpperCase();
});

console.log(str);
// EIfmEmp*3

Note: I removed the i modifier from the regex, since any captial letters will already be uppercase. This will save performance on a very minor scale (since we won't run E.toUpperCase(); since it is already uppercase).


To address @mplungjan's comment, no..regex can't really do any logic in a replace. It can reference things and replace matches with static content. Any modification of matches needs to be done with a callback function in your language of choice, though. We can do 5 separate replace calls:

str = str.replace(/a/g, 'A');
str = str.replace(/e/g, 'E');
str = str.replace(/i/g, 'I');
str = str.replace(/o/g, 'O');
str = str.replace(/u/g, 'U');

console.log(str);
// EIfmEmp*3

While the 5 simple replacements is faster, I prefer code readability ;)

Upvotes: 4

James
James

Reputation: 22247

For your map question:

var new_arr = [ 'e', 'i', 'f', 'm', 'e', 'm', 'p', '*', '3' ];

function vowelToUpCase(value, index, originalArray) {
    if(/[aeiou]/.test(value)) {
        return value.toUpperCase();
    }
    return value;
}

var fixed = new_arr.map(vowelToUpCase); // adding params here is bad
console.log(fixed.join(""));

Upvotes: 1

Gabriele Romanato
Gabriele Romanato

Reputation: 84

Here's a working example: CodePen

Hope this helps.

Upvotes: 0

Related Questions