Last Updated: February 25, 2016
· bitkomponist

javascript string replace

to get a string replace functionality do as follows:

var myString = "javascript can be used in a browser"

myString = myString.split("in a browser").join("everywhere");

//new content of myString: javascript can be used everywhere

example usage in a reallife scenario

function replaceMarkers(str,markers){
    var marker;
    for(marker in markers){
            str = str.split("{{"+marker+"}}").join(markers[marker]);
    return str;

var myString = replaceMarkers(
    "Hi, my name is {{firstname}} {{lastname}}"
    ,{"firstname":"John", "lastname":"Doe"}
console.log(myString); // outputs: Hi, my name is John Doe