String prototype substrReplace function str2 offset len return this su

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
String.prototype.substrReplace = function (str2, offset, len) {
return this.substring(0, offset) + str2 + this.substring(offset + len);
}
var text = (function () {/*
<a href="{{link_url}}">
<img src="{{img_url}}" />
</a>
*/}).toString().match(/[^]*\/\*([^]*)\*\/\}$/)[1];
var params = {
link_url: "http://spaces.ru",
img_url: "http://spaces.ru/i/lol.gif"
};
var m, var_re = /\{\{\s*([\w\d-_\$]+)\s*\}\}/gim, offset = 0;
while (m = var_re.exec(text)) {
var value = params[m[1]] || "";
text = text.substrReplace(value, m.index + offset, m[0].length);
var_re.lastIndex = m.index + value.length;
}
console.log(text);