Is it possible to read substring from char to char

You can use a simple regular expression with JavaScript’s string.replace(regex, callback):

var str = "Good Morning #:name:# and #:friend:#";
var replacements = {
    name: "Jake",
    friend: "Finn"
}

str = str.replace(/#:(.+?):#/g, function(match, id) {
    return replacements[id];
});

str.replace(regex, callback) will look for parts of the string str that match the pattern regex, and will replace it by what callback returns for each instance.

Leave a Comment