Extended JS Format function and Comment tree

Source: Internet
Author: User

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.