Ruby's Louvre

每天学习一点点算法

导航

给文字加上着重号

      var s = 'ABCDEFGHIJKLMNOPQRSTUVWXYZ01234567890';
      function f(s, width){
        return s.replace(
        new RegExp('[\\s\\S]{1,'+(width || 4)+'}', 'g'),
        function(m){
          return '^' + new Array(m.length).join(' ');
        });
      }

      console.log(s);
      console.log(f(s, 4));
      console.log(f(s, 8));

posted on 2011-04-10 16:01  司徒正美  阅读(1672)  评论(1编辑  收藏  举报