企业🤖AI Agent构建引擎,智能编排和调试,一键部署,支持私有化部署方案 广告
## 一、字符串重复 ``` String.prototype.repeat = function(n) { var str = ''; for(var i=0; i<n; i++) { str += this; } return str; } 'abc'.repeat(3); // abcabcabc ``` ## 二、去除字符串两边空格 ~~~ // 方式1 String.prototype.trim=function(){   return this.replace(/(^\s*)|(\s*$)/g, ""); } console.log(' hello '.trim()); // hello // 方式2 console.log(' hello '.replace(/^\s+|\s+$/g, '')); // hello ~~~ ## 三、去除规则字符串最后一个字符,以逗号为例 ~~~ var str = 'a,b,c,'; // 方式1 str = str.substr(0, str.lastIndexOf(',')); // a,b,c // 方式2 str.replace(/,$/, ''); // a,b,c ~~~