//String.prototype使用
//批量替换,比如:str.ReplaceAll([/a/g,/b/g,/c/g],["aaa","bbb","ccc"])
String.prototype.ReplaceAll=function (A,B) {
var C=this;
for(var i=0;i<A.length;i++) {
C=C.replace(A[i],B[i]);
};
return C;
};
// 去掉字符两端的空白字符
String.prototype.Trim=function () {
return this.replace(/(^[\t\n\r]*)|([\t\n\r]*$)/g,'');
};
// 去掉字符左边的空白字符
String.prototype.LTrim=function () {
return this.replace(/^[\t\n\r]/g,'');
};
// 去掉字符右边的空白字符
String.prototype.RTrim=function () {
return this.replace(/[\t\n\r]*$/g,'');
};
// 返回字符的长度,一个中文算2个
String.prototype.ChineseLength=function()
{
return this.replace(/[^\x00-\xff]/g,"**").length;
};
// 判断字符串是否以指定的字符串结束
String.prototype.EndsWith=function (A,B) {
var C=this.length;
var D=A.length;
if(D>C)return false;
if(B) {
var E=new RegExp(A+'$','i');
return E.test(this);
}else return (D==0||this.substr(C-D,D)==A);
};
// 判断字符串是否以指定的字符串开始
String.prototype.StartsWith = function(str)
{
return this.substr(0, str.length) == str;
};
// 字符串从哪开始多长字符去掉
String.prototype.Remove=function (A,B) {
var s='';
if(A>0)s=this.substring(0,A);
if(A+B<this.length)s+=this.substring(A+B,this.length);
return s;
};
分享到:
相关推荐
String.prototype.padStart 符合ES2017规范的String.prototype.padStart填充程序。 如果不可用,请调用其“ shim”方法对String.prototype.padStart进行填充。 该软件包实现了接口。 它可以在ES3支持的环境中工作...
string.prototype.replaceall 用于String.prototype.replaceAll的ES Proposal规范填充程序。 如果不可用或不String.prototype.replaceAll调用其“ shim”方法对String.prototype.replaceAll进行填充。 该软件包...
String.prototype.trimEnd 符合ES2019规范的String.prototype.trimEnd填充程序。 如果不可用,则调用其“ shim”方法对String.prototype.trimEnd进行填充。 该软件包实现了接口。 它可以在ES3支持的环境中工作并...
String.prototype.trim 符合ES5规范的String.prototype.trim填充程序。 如果不可用,请调用其“ shim”方法对String.prototype.trim进行填充。 该软件包实现了接口。 它可以在ES3支持的环境中工作,并符合规范( ...
string.prototype.matchall 用于String.prototype.matchAll的ES Proposal规范填充程序。 如果不可用或不String.prototype.matchAll则调用其“ shim”方法对String.prototype.matchAll进行填充。 该软件包实现了...
String.prototype.padEnd 符合ES2017规范的String.prototype.padEnd填充程序。 如果不可用,则调用其“ shim”方法对String.prototype.padEnd进行填充。 该软件包实现了接口。 它可以在ES3支持的环境中工作并符合。 ...
所有 String 的实例都继承自 String.prototype. 任何String.prototype上的改变都会影响到所有的 String 实例。 2、上正文,js扩展String.prototype.format字符串拼接的功能,首先是基础功能的改造: String....
String.prototype.trimStart 符合ES2019规范的String.prototype.trimStart填充程序。 如果不可用,请调用其“ shim”方法对String.prototype.trimStart进行填充。 该软件包实现了接口。 它可以在ES3支持的环境中工作...
String.prototype.codePoints 针对String.prototype.codePoints的ECMAScript建议 地位 该提案处于第一阶段。 动机 对于包含高于0xFFFF的代码点的语言(例如ECMAScript语法本身),词法分析器需要能够在使用自己的...
String.Prototype.trim() trim()返回一个字符串两端空白字符被删除的新字符串,不影响原字符串。 第一种实现方式:理论上算是比较不错的 if (!String.prototype.trim) { String.prototype.trim = function () { ...
还可以使用String.prototype.repeat其他polyfills: ( 现在通过所有测试) 安装 通过 : npm install string.prototype.repeat 然后,在: var repeat = require ( 'string.prototype.repeat' ) ; 在浏览器...
String.prototype.trimLeft 符合ES2019规范的String.prototype.trimLeft填充器。 如果不可用,则调用其“ shim”方法对String.prototype.trimLeft进行填充。 该软件包实现了接口。 它可以在ES3支持的环境中工作并...
String.prototype.trimRight 符合ES2019规范的String.prototype.trimRight填充程序。 如果不可用,请调用其“ shim”方法对String.prototype.trimRight进行填充。 该软件包实现了接口。 它可以在ES3支持的环境中...
几年后,即2020年11月,添加了具有不同语义的String.prototype.at 。 请改用 。 串连 的健壮且经过优化的ES3兼容polyfill。 这段代码几乎完全是从@mathiasbynens出色的polyfill复制的,为我创建了该代码,以便它...
这个项目展示了如何使用`String.prototype.trimStart()`方法。 ## 使用方法 1. 查看`main.js`文件,查看`trimStart()`的用法。 2. 运行`main.js`,观察控制台输出。 要理解`trimStart()`,请关注以下代码: ``` ...
String.prototype.format 函数是 JS 中的 string.format 函数代码的核心部分,该函数将字符串作为原型链的一部分,从而可以使用该函数来格式化字符串。该函数的实现原理是使用正则表达式将变量的值插入到字符串中。 ...
String.prototype.padRight = String.prototype.rpad; var str = "6"; console.log(str.lpad(2, "0")); // 输出 06 console.log(str.padLeft(2, "0")); // 输出 06 ``` 该方法通过扩展 String.prototype.pad 方法,...
柯里化的String.prototype.split支持按字符串、正则表达式或函数进行拆分。 npm install string-split --save npm 统计信息 例子 要求 var split = require ( 'string-split' ) ; 完整的应用程序 split ( "." , ...
String.prototype.ChineseLength=function() { return this.replace(/[^\x00-\xff]/g,"**").length; } // 判断字符串是否以指定的字符串结束 String.prototype.EndsWith = function(str) { return this.substr...
String.prototype.Trim = function () { return this.replace(/(^\s*)|(\s*$)/g, ""); }; String.prototype.LTrim = function () { return this.replace(/(^\s*)/g, ""); }; String.prototype.RTrim = function ...