The replacement of perl-style variables provides better readability. In common javascript programming, there are many + numbers, which are dazzling. Good news: we can use regular expressions to convert programming styles.
The following example specifies that the string in $ {} is the variable name and defines the varReplace prototype function to replace the variable:
- 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