String 的扩展方法

String的扩展方法
String.prototype.方法名=function(){...}
基础知识字符串操作和正则表达式的应用
一、合并多个空白为一个空白
String.prototype.resetBlank = function () {
    return this.replace(/\s+/g, " ");
}
二、过滤空白
String.prototype.filterBlank = function () {
    return this.replace(/\s+/g, "");
}
三、除去左边空白
String.prototype.LTrim = function () {
    return this.replace(/^\s+/, "");
}
四、除去右边空白
String.prototype.RTrim = function () {
    return this.replace(/\s+$/g, "");
}
五、保留数字
String.prototype.getNum = function () {
    return this.replace(/[^\d]/g, "");
}
六、保留字母
String.prototype.getEn = function () {
    return this.replace(/[^A-Za-z]/g, "");
}
七、保留中文
String.prototype.getCn = function () {
   return this.replace(/[^\u4e00-\u9fa5\uf900-\ufa2d]/g, "")
}
八、得到字节长度
String.prototype.getRealLength = function () {
   return this.replace(/[^\x00-\xff]/g, "--").length;
}
九、从左截取指定长度的字串
String.prototype.leftSlice = function (n) {
   return this.slice(0, n);
}
十、从右截取指定长度的字串
String.prototype.rightSlice = function (n) {
    return this.slice(this.length - n);
}

<!DOCTYPE> <html> <head> <title>字符串扩展方法---www.cnblogs.com/kuikui</title> <script type="text/javascript"> //合并多个空白为一个空白 String.prototype.resetBlank = function () { return this.replace(/\s+/g, " "); } //过滤空白 String.prototype.filterBlank = function () { return this.replace(/\s+/g, ""); } //除去左边空白 String.prototype.LTrim = function () { return this.replace(/^\s+/, ""); } //除去右边空白 String.prototype.RTrim = function () { return this.replace(/\s+$/g, ""); } //保留数字 String.prototype.getNum = function () { return this.replace(/[^\d]/g, ""); } //保留字母 String.prototype.getEn = function () { return this.replace(/[^A-Za-z]/g, ""); } //保留中文 String.prototype.getCn = function () { return this.replace(/[^\u4e00-\u9fa5\uf900-\ufa2d]/g, "") } //得到字节长度 String.prototype.getRealLength = function () { return this.replace(/[^\x00-\xff]/g, "--").length; } //从左截取指定长度的字串 String.prototype.leftSlice = function (n) { return this.slice(0, n); } //从右截取指定长度的字串 String.prototype.rightSlice = function (n) { return this.slice(this.length - n); } </script> </head> <body> <script type="text/javascript"> var str1 = " 测试 Test 123456 ... "; var str2 = "abcdef"; var str3 = "<html>"; document.write("一、合并多个空格为一个空格<br/>"); document.write("<input style='background:#ff9999; width:100%; border-style:none' readonly='readonly' value='" + "|" + str1.resetBlank() + "|' /><br/>"); document.write("二、过滤空白<br/>"); document.write("<input style='background:#ff9999; width:100%; border=0px; border-style:none' readonly='readonly' value='" + "|" + str1.filterBlank() + "|' /><br/>"); document.write("三、除去左边的空白<br/>"); document.write("<input style='background:#ff9999; width:100%; border=0px; border-style:none' readonly='readonly' value='" + "|" + str1.LTrim() + "|' /><br/>"); document.write("四、除去右边的空白<br/>"); document.write("<input style='background:#ff9999; width:100%; border=0px; border-style:none' readonly='readonly' value='" + "|" + str1.RTrim() + "|' /><br/>"); document.write("五、保留数字<br/>"); document.write("<input style='background:#ff9999; width:100%; border=0px; border-style:none' readonly='readonly' value='" + "|" + str1.getNum() + "|' /><br/>"); document.write("六、保留字母<br/>"); document.write("<input style='background:#ff9999; width:100%; border=0px; border-style:none' readonly='readonly' value='" + "|" + str1.getEn() + "|' /><br/>"); document.write("七、保留中文<br/>"); document.write("<input style='background:#ff9999; width:100%; border=0px; border-style:none' readonly='readonly' value='" + "|" + str1.getCn() + "|' /><br/>"); document.write("八、得到字节长度,一个汉字为两个字节<br/>"); document.write("<input style='background:#ff9999; width:100%; border=0px; border-style:none' readonly='readonly' value='" + "|" + str1.getRealLength() + "|' /><br/>"); document.write("九、从左截取n个字符<br/>"); document.write("<input style='background:#ff9999; width:100%; border=0px; border-style:none' readonly='readonly' value='" + "|" + str2.leftSlice(3) + "|' /><br/>"); document.write("十、从左截取n个字符<br/>"); document.write("<input style='background:#ff9999; width:100%; border=0px; border-style:none' readonly='readonly' value='" + "|" + str2.rightSlice(3) + "|' /><br/>"); </script> </body> </html>

运行代码

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值