将docx中的longtable转为html中的table标签
function toTable(str){
var regx = /\\begin{longtable}.*?\r\n\\toprule\r\n(.*?)\r\n\\midrule\r\n(.|\r\n)*?\\endhead\r\n((\s|\S)*?)\\bottomrule\r\n(.|\r\n)*?\\end{longtable}/g;
str = str.replace(regx,"<table><tbody><tr>$1</tr><tr>$3</tr></tbody></table>");
var regx2 = /(<table><tbody><tr>)(.*?)(<\/tr><tr>)((\s|\S)*?)(<\/tr><\/tbody><\/table>)/g;
str = str.replace(regx2,function(match){
var tmpstr = match;
var group = tmpstr.match(/<tr>((\s|\S)*?)<\/tr>/g);
var tmp1 = group[0].replace(/(<tr>)|(<\/tr>)/g,"");
var tmp2 = group[1].replace(/(<tr>)|(<\/tr>)/g,"");
tmp1 = tmp1.replace(/&(([^&]*)?)\\tabularnewline/g,"& $1 &");
tmp1 = tmp1.replace(/(.*?)&/g,"<td>$1</td>");
tmp2 = tmp2.replace(/&(([^&]*)?)\\tabularnewline/g,"& $1 &");
tmp2 = tmp2.replace(/(.*?)&/g,"<td>$1</td>");
return "<table border='1' cellspacing='0' cellpadding='5px' style='width:auto'><tbody><tr>" + tmp1 +"</tr><tr>"+ tmp2 +"</tr></tbody></table>";
});
return str;
}
第一步是提取longtable的内容,第二步用了个replace的小技巧,替代后的参数是一个函数。