热门标签 | HotTags
当前位置:  开发笔记 > 编程语言 > 正文

JavaScriptstring字符串类型的扩展方法(搜集整理)

1.Js判断前缀后缀代码和判空(含全部是空格):Js判断后缀String.prototype.endsWithfunction(suffix){ret

1. Js 判断前缀后缀代码和判空(含全部是空格):

// Js 判断后缀 
String.prototype.endsWith = function(suffix) {  
    return this.indexOf(suffix, this.length - suffix.length) !== -1;  
};  

// Js 判断前缀
if (typeof String.prototype.startsWith != 'function') {
  // see below for better implementation!
  String.prototype.startsWith = function (str){
    return this.indexOf(str) == 0;
  };
}  
  
// Js 判空(含全部是空格)
String.prototype.IsNullEmptyOrSpace = function()  
{   
   if (this== null) return true;  
      return this.replace(/s/g, '').length == 0;  
};  


2. 去前后空格

//去除左边的空格

*/
String.prototype.LTrim  function()
{
        return this.replace(/(^\s*)/g, "");
}
 
/*

//去除右边的空格

*/
String.prototype.Rtrim  function()
{
        return this.replace(/(\s*$)/g, "");
}
/*

//去除前后空格

*/
String.prototype.Trim  function()
{
        return this.replace(/(^\s*)|(\s*$)/g, "");
}
/*


 

3. 取子串

//得到左边的字符串

*/
String.prototype.Left  function(len)
{
        if(isNaN(len)||lennull)
        {
                len  this.length;
        }
        else
        {
                if(parseInt(len)<0||parseInt(len)>this.length)
                {
                        len  this.length;
                }
        }
         
        return this.substr(0,len);
}
 
/*

//得到右边的字符串

*/
String.prototype.Right  function(len)
{
        if(isNaN(len)||lennull)
        {
                len  this.length;
        }
        else
        {
                if(parseInt(len)<0||parseInt(len)>this.length)
                {
                        len  this.length;
                }
        }
         
        return this.substring(this.length-len,this.length);
}
 
/*

//得到中间的字符串,注意从0开始

*/
String.prototype.Mid  function(start,len)
{
        return this.substr(start,len);
}
 
/*

//在字符串里查找另一字符串:位置从0开始

*/
String.prototype.InStr  function(str)
{
        if(strnull)
        {
                str  "";
        }
         
        return this.indexOf(str);
}
/*

//在字符串里反向查找另一字符串:位置0开始

*/
String.prototype.InStrRev  function(str)
{
        if(strnull)
        {
                str  "";
        }
         
        return this.lastIndexOf(str);
}
/*


4. 验证常用格式

 

//是否是正确的IP地址

*/
String.prototype.isIP  function()
{
        var reSpaceCheck  /^(\d+)\.(\d+)\.(\d+)\.(\d+)$/;
        if (reSpaceCheck.test(this))
        {
                this.match(reSpaceCheck);
                if (RegExp.$1 <255 && RegExp.$1 > 0 
                 && RegExp.$2 <255 && RegExp.$2 > 0 
                 && RegExp.$3 <255 && RegExp.$3 > 0 
                 && RegExp.$4 <255 && RegExp.$4 > 0) 
                {
                        return true;     
                }
                else
                {
                        return false;
                }
        }
        else
        {
                return false;
        }
    
}
 
/*

//是否是正确的长日期

*/
String.prototype.isLongDate  function()
{
        var r  this.replace(/(^\s*)|(\s*$)/g, "").match(/^(\d{1,4})(-|\/)(\d{1,2})\2(\d{1,2}) (\d{1,2}):(\d{1,2}):(\d{1,2})$/); 
        if(rnull)
        {
                return false; 
        }
        var d  new Date(r[1], r[3]-1,r[4],r[5],r[6],r[7]); 
        return (d.getFullYear()r[1]&&(d.getMonth()+1)r[3]&&d.getDate()r[4]&&d.getHours()r[5]&&d.getMinutes()r[6]&&d.getSeconds()r[7]);
}
/*

//是否是正确的短日期

*/
String.prototype.isShortDate  function()
{
        var r  this.replace(/(^\s*)|(\s*$)/g, "").match(/^(\d{1,4})(-|\/)(\d{1,2})\2(\d{1,2})$/); 
        if(rnull)
        {
                return false; 
        }
        var d  new Date(r[1], r[3]-1, r[4]); 
        return (d.getFullYear()r[1]&&(d.getMonth()+1)r[3]&&d.getDate()r[4]);
}
/*

//是否是正确的日期

*/
String.prototype.isDate  function()
{
        return this.isLongDate()||this.isShortDate();
}
/*

//是否是手机

*/
String.prototype.isMobile  function()
{
        return /^0{0,1}13[0-9]{9}$/.test(this);
}
/*

//是否是邮件

*/
String.prototype.isEmail  function()
{
        return /^\w+((-\w+)|(\.\w+))*\@[A-Za-z0-9]+((\.|-)[A-Za-z0-9]+)*\.[A-Za-z0-9]+$/.test(this);
}
/*

//是否是邮编(中国)

*/
String.prototype.isZipCode  function()
{
        return /^[\\d]{6}$/.test(this);
}
/*

//是否是有汉字

*/
String.prototype.existChinese  function()
{
        //[\u4E00-\u9FA5]為漢字﹐[\uFE30-\uFFA0]為全角符號
        return /^[\x00-\xff]*$/.test(this);
}
/*

//是否是合法的文件名/目录名

*/
String.prototype.isFileName  function()
{
        return !/[\\\/\*\?\|:"<>]/g.test(this);
}
/*

//是否是有效链接

*/
String.prototype.isUrl  function()
{
        return /^http[s]?:\/\/([\w-]+\.)+[\w-]+([\w-./?%&]*)?$/i.test(this);
}
 
/*

//是否是有效的身份证(中国)

*/
String.prototype.isIDCard  function()
{
        var iSum0;
        var info"";
        var sId  this;
        var aCity{11:"北京",12:"天津",13:"河北",14:"山西",15:"内蒙古",21:"辽宁",22:"吉林",23:"黑龙江",31:"上海",32:"江苏",33:"浙江",34:"安徽",35:"福建",36:"江西",37:"山东",41:"河南",42:"湖北",43:"湖南",44:"广东",45:"广西",46:"海南",50:"重庆",51:"四川",52:"贵州",53:"云南",54:"西藏",61:"陕西",62:"甘肃",63:"青海",64:"宁夏",65:"新疆",71:"台湾",81:"香港",82:"澳门",91:"国外"};
        if(!/^\d{17}(\d|x)$/i.test(sId))
        {
                return false;
        }
        sIdsId.replace(/x$/i,"a");
        //非法地区
        if(aCity[parseInt(sId.substr(0,2))]null)
        {
                return false;
        }
        var sBirthdaysId.substr(6,4)+"-"+Number(sId.substr(10,2))+"-"+Number(sId.substr(12,2));
        var dnew Date(sBirthday.replace(/-/g,"/"))
         
        //非法生日
        if(sBirthday!(d.getFullYear()+"-"+ (d.getMonth()+1) + "-" + d.getDate()))
        {
                return false;
        }
        for(var i  17;i>0;i--) 
        {
                iSum + (Math.pow(2,i) % 11) * parseInt(sId.charAt(17 - i),11);
        }
        if(iSum%11!1)
        {
                return false;
        }
        return true;
}
/*

//是否是有效的电话号码(中国)

*/
String.prototype.isPhoneCall  function()
{
        return /(^[0-9]{3,4}\-[0-9]{3,8}$)|(^[0-9]{3,8}$)|(^\([0-9]{3,4}\)[0-9]{3,8}$)|(^0{0,1}13[0-9]{9}$)/.test(this);
}
 
/*

//是否是数字

*/
String.prototype.isNumeric  function(flag)
{
        //验证是否是数字
        if(isNaN(this))
        {
                return false;
        }
        switch(flag)
        {
                case null:        //数字
                case "":
                        return true;
                case "+":        //正数
                        return                /(^\+?|^\d?)\d*\.?\d+$/.test(this);
                case "-":        //负数
                        return                /^-\d*\.?\d+$/.test(this);
                case "i":        //整数
                        return                /(^-?|^\+?|\d)\d+$/.test(this);
                case "+i":        //正整数
                        return                /(^\d+$)|(^\+?\d+$)/.test(this);                        
                case "-i":        //负整数
                        return                /^[-]\d+$/.test(this);
                case "f":        //浮点数
                        return                /(^-?|^\+?|^\d?)\d*\.\d+$/.test(this);
                case "+f":        //正浮点数
                        return                /(^\+?|^\d?)\d*\.\d+$/.test(this);                        
                case "-f":        //负浮点数
                        return                /^[-]\d*\.\d$/.test(this);                
                default:        //缺省
                        return true;                        
        }
}
/*

//是否是颜色(#FFFFFF形式)

*/
String.prototype.IsColor  function()
{
        var temp         this;
        if (temp"") return true;
        if (temp.length!7) return false;
        return (temp.search(/\#[a-fA-F0-9]{6}/) ! -1);
}
/*


 

5. 格式转换(全角,日期,Html编码)

//转换成全角

*/
String.prototype.toCase  function()
{
        var tmp  "";
        for(var i0;i0&&this.charCodeAt(i)<255)
                {
                        tmp + String.fromCharCode(this.charCodeAt(i)+65248);
                }
                else
                {
                        tmp + String.fromCharCode(this.charCodeAt(i));
                }
        }
        return tmp
}
/*

//对字符串进行Html编码

*/
String.prototype.toHtmlEncode  function()
{
        var str  this;
        strstr.replace(/&/g,"&");
        strstr.replace(//g,">");
        strstr.replace(/\'/g,"'");
        strstr.replace(/\"/g,""");
        strstr.replace(/\n/g,"
"); strstr.replace(/\ /g," "); strstr.replace(/\t/g,"    "); return str; } /* //转换成日期 */ String.prototype.toDate function() { try { return new Date(this.replace(/-/g, "\/")); } catch(e) { return null; } }


6.其他

//计算字符串打印长度

*/
String.prototype.LengthW  function()
{
        return this.replace(/[^\x00-\xff]/g,"**").length;
}
/*


 替换所有匹配的字串:

String.prototype.replaceAll function(find, replace) {
  return this.replace(new RegExp(find, 'g'), replace);
}


 


推荐阅读
  • 8.2location对象location对象既是window对象的属性,也是document对象的属性.window.location和document.location引用的是同一个对象. ... [详细]
  • 基础数据范例ECMAScript中有5种简朴数据范例(也称基础数据范例):Undefined,Null,Boolean,Number和String。另有一种庞杂数据范例(援用型)O ... [详细]
  • 用了2周,把jquery2.1.1版本的源码读了一遍,像得了感冒,头疼、恶心、没精神。。。涉及javaScript基本知识点: ... [详细]
  • Python正则表达式学习记录及常用方法
    本文记录了学习Python正则表达式的过程,介绍了re模块的常用方法re.search,并解释了rawstring的作用。正则表达式是一种方便检查字符串匹配模式的工具,通过本文的学习可以掌握Python中使用正则表达式的基本方法。 ... [详细]
  • 利用Visual Basic开发SAP接口程序初探的方法与原理
    本文介绍了利用Visual Basic开发SAP接口程序的方法与原理,以及SAP R/3系统的特点和二次开发平台ABAP的使用。通过程序接口自动读取SAP R/3的数据表或视图,在外部进行处理和利用水晶报表等工具生成符合中国人习惯的报表样式。具体介绍了RFC调用的原理和模型,并强调本文主要不讨论SAP R/3函数的开发,而是针对使用SAP的公司的非ABAP开发人员提供了初步的接口程序开发指导。 ... [详细]
  • 巧用arguments在Javascript的函数中有个名为arguments的类数组对象。它看起来是那么的诡异而且名不经传,但众多的Javascript库都使用着它强大的功能。所 ... [详细]
  • 包含A-Z的字母的消息通过以下规则编码:'A'-1'B'-2'Z'-26给定一个包含数字的编码消息,请确定解 ... [详细]
  • 这期内容当中小编将会给大家带来有关如何在php表单中使用正则表达式,文章内容丰富且以专业的角度为大家分析和叙述,阅读完这篇文章希望大家可 ... [详细]
  • 表单提交前的最后验证:通常在表单提交前,我们必须确认用户是否都把必须填选的做了,如果没有,就不能被提交到服务器,这里我们用到表单的formname.submit()看演示,其实这个对于我们修炼道 ... [详细]
  • 字面|开辟_一文入门JavaScript
    篇首语:本文由编程笔记#小编为大家整理,主要介绍了一文入门JavaScript相关的知识,希望对你有一定的参考价值。@toc ... [详细]
  • 实现时主要问题在于怎么将所有对象给找出来,替换成user.name的形式。Overridepublicvoidsave(Comme ... [详细]
  • [JavaScript] 多数前端工程师都没注意到的一个关于console.log()的坑
    [JavaScript]多数前端工程师都没注意到的一个关于console.log()的坑请阅读以下代码并猜测结果:functiontest(){le ... [详细]
  • JavaScript - let和var区别
    前提ES5只有函数作用域和全局作用域,var属于ES5。let属于ES6,新增块级作用域。目的是可以写更安全的代码。Theletstatementdeclaresablocks ... [详细]
  • 但有时候,需要当某事件触发时,我们先做一些操作,然后再跳转,这时,就要用JAVASCRIPT来实现这一跳转功能。下面是具体的做法:一:跳转到新页面,并且是在新窗口中打开时:复制代码代码如下:fu ... [详细]
  • Whyusingstringsaskeysofarray,consoleisshowingthatarraywithoutthesedeclaredvaluesand ... [详细]
author-avatar
润滑油一_576
这个家伙很懒,什么也没留下!
PHP1.CN | 中国最专业的PHP中文社区 | DevBox开发工具箱 | json解析格式化 |PHP资讯 | PHP教程 | 数据库技术 | 服务器技术 | 前端开发技术 | PHP框架 | 开发工具 | 在线工具
Copyright © 1998 - 2020 PHP1.CN. All Rights Reserved | 京公网安备 11010802041100号 | 京ICP备19059560号-4 | PHP1.CN 第一PHP社区 版权所有