Perl-style variable substitution has better readability, and common JavaScript programming with a lot of + number, dazzling. The good news is that we can use regular expressions to help implement a programming-style transformation.
The following example stipulates that the string within ${} is a variable name, and the Varreplace prototype function is defined to implement variable substitution:
String.prototype.varReplace = function () {
var pttrn =/\${(\w+)}/g;
Return This.replace (Pttrn,function (a,b) {return eval (b)});
}
var myVar1 = "#00ff11";
var myVar2 = "Test";
var myStr1 = ' <font color= ' ${myvar1} ' ><b>${myVar2}</b></font> '//perl style
var myStr2 = ' <font color= "' +myvar1+ '" ><b> ' +myvar2+ ' </b></font> ';//javascript style
ijs.pt (" Mystr1.varreplace () ");
ijs.pt ("MyStr2");
Debugging information:
Mystr1.varreplace () test
MYSTR2 Test
This article is from the "idata" blog, please be sure to keep this source http://idata.blog.51cto.com/4581576/1108042