ãã¼ã«ã«å¤æ°ãå±éãããï¼
å¼¾ããã®ã¨ãã§ãJavaScriptã§ããã¹ãå ã®å¤æ°å±éãããæ¹æ³ãã§ã¦ãã
ããã¹ãå ã®å¤æ°å±éã¯ãã¤ã欲ããã¨æãã+ã§æååãé£çµããã®ããã!ã¨ããããã§ä½ã£ã¦ã¿ã¾ããã
function interpolate(str){ return str.replace(/#\{([^\}]+)\}/g, function(m0,m1){ var v = ''; try{ v = eval(m1); }catch(e){ v = e } return v; }); }http://blog.livedoor.jp/dankogai/archives/50766443.html
ã§ãã
å çããããããã¼ã«ã«å¤æ°ãå±éã§ãã¾ããï¼
ãªã®ã§ããããªæãã§ãããã§ãããï¼
function interpolate(str) { return "\""+ str.replace(/\\/g, '\\\\').replace(/"/g, '\\"').replace(/#{(.*?)}/g, "\"+($1)+\"") +"\""; }
使ãæ¹ã¯ãã
var today = new Date(); alert(eval(interpolate("ããã«ã¡ã¯ãç¾å¨ã¯#{today.toLocaleString()}ã§ãã")));
interpolateé¢æ°ã¯å¤æ°ãå¼ã®åãè¾¼ã¾ããããã¹ãã+ã使ãæååé£çµã®ã¹ã¯ãªããããã ãã«ãã¦ãå¼ã³å
ã§evalããã°ãã¼ã«ã«å¤æ°ãå±éå¯è½ã«ãªãã¨ããããã§ãã
evalãå¤å´ã«ããåãã¡ãã£ã¨ä¸æ°å¥½ã§ãããã®è¾ºã¯ã¬ãã³ãã¦ãã ããï½