Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Javascript str_replace many at once

Tags:

javascript

Is there any straightforward manner to make a character replacing in javascript of many chars in just one instruction with different replacement for each one, like it is possible in PHP?

I mean, something like:

replace('áéíóú', 'aeiou');

That replaces á with a, é with e, í with i, and so on...

Thanks a lot in advance,

like image 740
Fran Marzoa Avatar asked Jul 26 '26 00:07

Fran Marzoa


1 Answers

Use regex with the global flag:

var map = {
    "á": "a",
    "é": "e",
    "í": "i",
    "ó": "o",
    "ú": "u"
};

"áéíóú".replace(/[áéíóú]/g, function(m){
    return map[m];
});
like image 63
Esailija Avatar answered Jul 28 '26 13:07

Esailija



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!