1. js Format function <script> /* 1. when calling an object method, you can extend the 2. by invoking the method in the prototype of the class. Regular Expressions /\w+/g 3. string replace ". Replace (' Alex ', ' SB '); ". Replace (/\w+/, ' SB '); ". Replace (/\w +/g, ' SB '); '. Replace (/(\w+)/g,function (K,KK) {return 11;}); */ String.prototype.format = function (ARG) {&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBsp; /* this, Current string "I am {name1}, age is {age9}" arg,format method passed in parameter {name: ' Alex ',age:18} return, the new content obtained after formatting i am alex, age is 18 */ var temp = this.replace (/\{(\w+) \}/g, function (K,KK) { return arg[kk]; }); return temp; };
2, using the above formatting function and recursive implementation of the comment tree
Function commenttree (commentlist) { var comment_str = "<div class= ' comment ' >"; $.each (Commentlist,function (k,row) { // var temp = "<div class= ' content ' > ' + row.content + ' </div> '; var temp = "<div class= ' content ' >{ Content}</div> ". Format ({content:row.content}); comment_str += temp; if (row.child.length>0) { comment_str += commenttree ( Row.child); } }); comment_str += ' </div> '; return comment_str; }
Extended JS Format function and Comment tree