When making template content output, placeholders such as <%=%> are often used. It is very convenient to replace this content with regular methods. In my work, I only come into contact with java, php, and js, three languages Simple implementation:
java version
import java.util.HashMap;
import java.util.Map;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
public class TestRex {
public static void main(String[] args) {
Map map = new HashMap();
map.put("name", "penngo");
map.put("date", "2013-01-17");
Pattern p = Pattern.compile("<%=(\w+?)%>");
String str = "<%=date%>, Hello <%=name%>";
Matcher m = p.matcher(str);
StringBuffer sb = new StringBuffer();
boolean result = m.find();
while (result) {
String key = m.group(1);
String value = map.get(key);
m.appendReplacement(sb, value);
result = m.find();
}
m.appendTail(sb);
System.out.println(sb.toString());
}
}
php version
$data = array('name'=>'penngo', 'date'=>'2013-01-17');
function replaceStr($key, $data){
return $data[$key];
}
$str = '<%=date%>, Hello <%=name%>';
$str = preg_replace("/<%=(w+?)%>/ise", "replaceStr('\1', $data)", $str);
echo $str;
?>
js version
var date = {'name':'penngo', 'date':'2013-01-17'};
var str = '<%=date%>, Hello <%=name%>';
str = str.replace(/<%=(w+?)%>/g, function($0, $1){
var value = date[$1];
return value;
});www.2cto.com
document.write(str);
Run result output:
2013-01-17, Hello penngo
http://www.bkjia.com/PHPjc/477810.htmlwww.bkjia.comtruehttp: //www.bkjia.com/PHPjc/477810.htmlTechArticleWhen producing template content output, placeholders such as %=% are often used, and regular The method is very convenient to replace this content. At work, I only come into contact with java, php, and js. The three languages are simple...