Dictionary replace with Javascript

Simple dictionary replace with javascript.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
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!
X

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