Dictionary replace with Javascript

Simple dictionary replace with javascript.
function dictionaryReplace(str, dictionary) { return str.replace(/w+/g, function(regex_match) { return dictionary[regex_match] || regex_match; }) } var dictionary = {"Hello": "Herzlich willkommen,", "world": "lieber Besucher!"}; var my_string = "Das ist ein Test: Hello world"; alert(dictionaryReplace(my_string, dictionary)); //Das ist ein Test: Herzlich willkommen, lieber Besucher!

Language: JavaScript | User: sklueh | Created: Sep 16, 2013