ylbtech-JavaScript-util: util.string.js

字符串处理工具

1.A,JS-效果图返回顶部
 
1.B,JS-Source Code(源代码)返回顶部
1.B.1, m.yintai
  1. /*
  2. ******************************************
  3. 字符串函数扩充
  4. ******************************************
  5. */
  6.  
  7. /*
  8. ===========================================
  9. //去除左边的空格
  10. ===========================================
  11.  
  12. */
  13. String.prototype.LTrim = function()
  14. {
  15. return this.replace(/(^\s*)/g, "");
  16. }
  17.  
  18. /*
  19. ===========================================
  20. //去除右边的空格
  21. ===========================================
  22. */
  23. String.prototype.Rtrim = function()
  24. {
  25. return this.replace(/(\s*$)/g, "");
  26. }
  27.  
  28. /*
  29. ===========================================
  30. //去除前后空格
  31. ===========================================
  32. */
  33. String.prototype.Trim = function()
  34. {
  35. return this.replace(/(^\s*)|(\s*$)/g, "");
  36. }
  37.  
  38. /*
  39. ===========================================
  40. //得到左边的字符串
  41. ===========================================
  42. */
  43. String.prototype.Left = function(len)
  44. {
  45.  
  46. if(isNaN(len)||len==null)
  47. {
  48. len = this.length;
  49. }
  50. else
  51. {
  52. if(parseInt(len)<0||parseInt(len)>this.length)
  53. {
  54. len = this.length;
  55. }
  56. }
  57.  
  58. return this.substr(0,len);
  59. }
  60.  
  61. /*
  62. ===========================================
  63. //得到右边的字符串
  64. ===========================================
  65. */
  66. String.prototype.Right = function(len)
  67. {
  68.  
  69. if(isNaN(len)||len==null)
  70. {
  71. len = this.length;
  72. }
  73. else
  74. {
  75. if(parseInt(len)<0||parseInt(len)>this.length)
  76. {
  77. len = this.length;
  78. }
  79. }
  80.  
  81. return this.substring(this.length-len,this.length);
  82. }
  83.  
  84. /*
  85. ===========================================
  86. //得到中间的字符串,注意从0开始
  87. ===========================================
  88. */
  89. String.prototype.Mid = function(start,len)
  90. {
  91. return this.substr(start,len);
  92. }
  93.  
  94. /*
  95. ===========================================
  96. //在字符串里查找另一字符串:位置从0开始
  97. ===========================================
  98. */
  99. String.prototype.InStr = function(str)
  100. {
  101.  
  102. if(str==null)
  103. {
  104. str = "";
  105. }
  106.  
  107. return this.indexOf(str);
  108. }
  109.  
  110. /*
  111. ===========================================
  112. //在字符串里反向查找另一字符串:位置0开始
  113. ===========================================
  114. */
  115. String.prototype.InStrRev = function(str)
  116. {
  117.  
  118. if(str==null)
  119. {
  120. str = "";
  121. }
  122.  
  123. return this.lastIndexOf(str);
  124. }
  125.  
  126. /*
  127. ===========================================
  128. //计算字符串打印长度
  129. ===========================================
  130. */
  131. String.prototype.LengthW = function()
  132. {
  133. return this.replace(/[^\x00-\xff]/g,"**").length;
  134. }
  135.  
  136. /*
  137. ===========================================
  138. //是否是正确的IP地址
  139. ===========================================
  140. */
  141. String.prototype.isIP = function()
  142. {
  143.  
  144. var reSpaceCheck = /^(\d+)\.(\d+)\.(\d+)\.(\d+)$/;
  145.  
  146. if (reSpaceCheck.test(this))
  147. {
  148. this.match(reSpaceCheck);
  149. if (RegExp.$1 <= 255 && RegExp.$1 >= 0
  150. && RegExp.$2 <= 255 && RegExp.$2 >= 0
  151. && RegExp.$3 <= 255 && RegExp.$3 >= 0
  152. && RegExp.$4 <= 255 && RegExp.$4 >= 0)
  153. {
  154. return true;
  155. }
  156. else
  157. {
  158. return false;
  159. }
  160. }
  161. else
  162. {
  163. return false;
  164. }
  165.  
  166. }
  167.  
  168. /*
  169. ===========================================
  170. //是否是正确的长日期
  171. ===========================================
  172. */
  173. String.prototype.isLongDate = function()
  174. {
  175. 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})$/);
  176. if(r==null)
  177. {
  178. return false;
  179. }
  180. var d = new Date(r[1], r[3]-1,r[4],r[5],r[6],r[7]);
  181. 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]);
  182.  
  183. }
  184.  
  185. /*
  186. ===========================================
  187. //是否是正确的短日期
  188. ===========================================
  189. */
  190. String.prototype.isShortDate = function()
  191. {
  192. var r = this.replace(/(^\s*)|(\s*$)/g, "").match(/^(\d{1,4})(-|\/)(\d{1,2})\2(\d{1,2})$/);
  193. if(r==null)
  194. {
  195. return false;
  196. }
  197. var d = new Date(r[1], r[3]-1, r[4]);
  198. return (d.getFullYear()==r[1]&&(d.getMonth()+1)==r[3]&&d.getDate()==r[4]);
  199. }
  200.  
  201. /*
  202. ===========================================
  203. //是否是正确的日期
  204. ===========================================
  205. */
  206. String.prototype.isDate = function()
  207. {
  208. return this.isLongDate()||this.isShortDate();
  209. }
  210.  
  211. /*
  212. ===========================================
  213. //是否是手机
  214. ===========================================
  215. */
  216. String.prototype.isMobile = function()
  217. {
  218. return /^0{0,1}13[0-9]{9}$/.test(this);
  219. }
  220.  
  221. /*
  222. ===========================================
  223. //是否是邮件
  224. ===========================================
  225. */
  226. String.prototype.isEmail = function()
  227. {
  228. return /^\w+((-\w+)|(\.\w+))*\@[A-Za-z0-9]+((\.|-)[A-Za-z0-9]+)*\.[A-Za-z0-9]+$/.test(this);
  229. }
  230.  
  231. /*
  232. ===========================================
  233. //是否是邮编(中国)
  234. ===========================================
  235. */
  236.  
  237. String.prototype.isZipCode = function()
  238. {
  239. return /^[\\d]{6}$/.test(this);
  240. }
  241.  
  242. /*
  243. ===========================================
  244. //是否是有汉字
  245. ===========================================
  246. */
  247. String.prototype.existChinese = function()
  248. {
  249. //[\u4E00-\u9FA5]為漢字﹐[\uFE30-\uFFA0]為全角符號
  250. return /^[\x00-\xff]*$/.test(this);
  251. }
  252.  
  253. /*
  254. ===========================================
  255. //是否是合法的文件名/目录名
  256. ===========================================
  257. */
  258. String.prototype.isFileName = function()
  259. {
  260. return !/[\\\/\*\?\|:"<>]/g.test(this);
  261. }
  262.  
  263. /*
  264. ===========================================
  265. //是否是有效链接
  266. ===========================================
  267. */
  268. String.prototype.isUrl = function()
  269. {
  270. return /^http[s]?:\/\/([\w-]+\.)+[\w-]+([\w-./?%&=]*)?$/i.test(this);
  271. }
  272.  
  273. /*
  274. ===========================================
  275. //是否是有效的身份证(中国)
  276. ===========================================
  277. */
  278. String.prototype.isIDCard = function()
  279. {
  280. var iSum=0;
  281. var info="";
  282. var sId = this;
  283.  
  284. 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:"国外"};
  285.  
  286. if(!/^\d{17}(\d|x)$/i.test(sId))
  287. {
  288. return false;
  289. }
  290. sId=sId.replace(/x$/i,"a");
  291. //非法地区
  292. if(aCity[parseInt(sId.substr(0,2))]==null)
  293. {
  294. return false;
  295. }
  296.  
  297. var sBirthday=sId.substr(6,4)+"-"+Number(sId.substr(10,2))+"-"+Number(sId.substr(12,2));
  298.  
  299. var d=new Date(sBirthday.replace(/-/g,"/"))
  300.  
  301. //非法生日
  302. if(sBirthday!=(d.getFullYear()+"-"+ (d.getMonth()+1) + "-" + d.getDate()))
  303. {
  304. return false;
  305. }
  306. for(var i = 17;i>=0;i--)
  307. {
  308. iSum += (Math.pow(2,i) % 11) * parseInt(sId.charAt(17 - i),11);
  309. }
  310.  
  311. if(iSum%11!=1)
  312. {
  313. return false;
  314. }
  315. return true;
  316.  
  317. }
  318.  
  319. /*
  320. ===========================================
  321. //是否是有效的电话号码(中国)
  322. ===========================================
  323. */
  324. String.prototype.isPhoneCall = function()
  325. {
  326. 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);
  327. }
  328.  
  329. /*
  330. ===========================================
  331. //是否是数字
  332. ===========================================
  333. */
  334. String.prototype.isNumeric = function(flag)
  335. {
  336. //验证是否是数字
  337. if(isNaN(this))
  338. {
  339.  
  340. return false;
  341. }
  342.  
  343. switch(flag)
  344. {
  345.  
  346. case null: //数字
  347. case "":
  348. return true;
  349. case "+": //正数
  350. return /(^\+?|^\d?)\d*\.?\d+$/.test(this);
  351. case "-": //负数
  352. return /^-\d*\.?\d+$/.test(this);
  353. case "i": //整数
  354. return /(^-?|^\+?|\d)\d+$/.test(this);
  355. case "+i": //正整数
  356. return /(^\d+$)|(^\+?\d+$)/.test(this);
  357. case "-i": //负整数
  358. return /^[-]\d+$/.test(this);
  359. case "f": //浮点数
  360. return /(^-?|^\+?|^\d?)\d*\.\d+$/.test(this);
  361. case "+f": //正浮点数
  362. return /(^\+?|^\d?)\d*\.\d+$/.test(this);
  363. case "-f": //负浮点数
  364. return /^[-]\d*\.\d$/.test(this);
  365. default: //缺省
  366. return true;
  367. }
  368. }
  369.  
  370. /*
  371. ===========================================
  372. //是否是颜色(#FFFFFF形式)
  373. ===========================================
  374. */
  375. String.prototype.IsColor = function()
  376. {
  377. var temp = this;
  378. if (temp=="") return true;
  379. if (temp.length!=7) return false;
  380. return (temp.search(/\#[a-fA-F0-9]{6}/) != -1);
  381. }
  382.  
  383. /*
  384. ===========================================
  385. //转换成全角
  386. ===========================================
  387. */
  388. String.prototype.toCase = function()
  389. {
  390. var tmp = "";
  391. for(var i=0;i<this.length;i++)
  392. {
  393. if(this.charCodeAt(i)>0&&this.charCodeAt(i)<255)
  394. {
  395. tmp += String.fromCharCode(this.charCodeAt(i)+65248);
  396. }
  397. else
  398. {
  399. tmp += String.fromCharCode(this.charCodeAt(i));
  400. }
  401. }
  402. return tmp
  403. }
  404.  
  405. /*
  406. ===========================================
  407. //对字符串进行Html编码
  408. ===========================================
  409. */
  410. String.prototype.toHtmlEncode = function()
  411. {
  412. var str = this;
  413.  
  414. str=str.replace(/&/g,"&amp;");
  415. str=str.replace(/</g,"&lt;");
  416. str=str.replace(/>/g,"&gt;");
  417. str=str.replace(/\'/g,"&apos;");
  418. str=str.replace(/\"/g,"&quot;");
  419. str=str.replace(/\n/g,"<br>");
  420. str=str.replace(/\ /g,"&nbsp;");
  421. str=str.replace(/\t/g,"&nbsp;&nbsp;&nbsp;&nbsp;");
  422.  
  423. return str;
  424. }
  425.  
  426. /*
  427. ===========================================
  428. //转换成日期
  429. ===========================================
  430. */
  431. String.prototype.toDate = function()
  432. {
  433. try
  434. {
  435. return new Date(this.replace(/-/g, "\/"));
  436. }
  437. catch(e)
  438. {
  439. return null;
  440. }
  441. }

1.B.2,

1.C,JS-Relevent References(相关引用)返回顶部
作者:ylbtech
出处:http://ylbtech.cnblogs.com/
本文版权归作者和博客园共有,欢迎转载,但未经作者同意必须保留此段声明,且在文章页面明显位置给出原文连接,否则保留追究法律责任的权利。

util.string.js的更多相关文章

  1. util.select.js

    ylbtech-JavaScript-util: util.select.js 筛选工具 1.A,JS-效果图返回顶部   1.B,JS-Source Code(源代码)返回顶部 1.B.1, m.y ...

  2. util.date.js

    ylbtech-JavaScript-util: util.date.js 日期处理工具 1.A,JS-效果图返回顶部   1.B,JS-Source Code(源代码)返回顶部 1.B.1, m.y ...

  3. jquery.string.js

    /** * jquery.string - Prototype string functions for jQuery * version: 1.1.0 * (c) 2008-2011 David E ...

  4. nodejs中的util.inspect.js

    util.inspect(object,[showHidden],[depth],[colors])是一个将任意对象转换 为字符串的方法,通常用于调试和错误输出.它至少接受一个参数 object,即要 ...

  5. String js删除字符串的最后一个字符三种方法

    字符串 var basic = "abc,def,ghi,"; 第一种 basic = basic.substr(0, basic.length - 1); 第二种 basic = ...

  6. js基础篇string&&array(应YX同学面试复习要求 - -)

    js中的数据类型一共有五个基本数据类型,分别是undefined,null,boolean,number,string. js中的Object类型中包括两大类型:Function类型和array类型. ...

  7. node.js 下使用 util.inherits 来实现继承

    上一篇博客说到了node.js继承events类实现事件发射和事件绑定函数,其中我们实现了一个公用基类 _base ,然后在模型中差异化的定义了各种业务需要的模型并继承 _base 公共基类.但是其中 ...

  8. js uppercase the first letter of string

    js uppercase the first letter of string js String.toUpperCase `-webkit-border-image`.split(`-`).filt ...

  9. Rhino+envjs-1.2.js 在java运行网站js 工具类

    java爬虫遇到个页面加密的东西,找了些资料学习学习 做了个java运行js的工具类,希望对大家有用,其中用到client(获取js)可以自行换成自己的client.主要是用了 Rhino就是Java ...

随机推荐

  1. 写把proto函数搞清楚

    在做blk层之前,先把proto搞清楚 ffi_lua metatype可以给函数加方法, lua中冒号是啥意思?冒号会传入self,但是点号不会传入self

  2. 在Eclipse中调用weka包实现分类

    1.如题. 最近写了一个FCM的聚类算法,希望能够可视化结果,因此一个想法是调用weka中的包,使自己的程序可以可视化.这里参考了网络上的方法,首先实现在Eclipse中调用weka包实现分类的功能. ...

  3. a标签打电话

    <a href="tel:0147-88469258"></a> <a href="mailto:bd@pangxiekeji.com&qu ...

  4. JS省份联级下拉框

    <script type="text/javascript"> var china=new Object();china['北京市']=new Array('北京市区' ...

  5. noip 2010 三国游戏

    三国游戏 三国游戏 描述 小涵很喜欢电脑游戏,这些天他正在玩一个叫做<三国>的游戏. 在游戏中,小涵和计算机各执一方,组建各自的军队进行对战.游戏中共有N 位武将(N为偶数且不小于4),任 ...

  6. NGINX: 优化 use 参数

    转自:http://blog.sina.com.cn/s/blog_5eaf88f10100gkrq.html Nginx use参数分析对比 下图对比了poll select epoll和kqueu ...

  7. .NET 4.0中使用sqlite

    原文发布时间为:2011-12-10 -- 来源于本人的百度文章 [由搬家工具导入] http://www.csharpcity.com/sqlite-ado-net-c-4-0/ Getting S ...

  8. [ CodeVS冲杯之路 ] P1501

     不充钱,你怎么AC? 题目:http://codevs.cn/problem/1501/ 水题一道 直接dfs,记录上当前深度,到了叶子节点就更新答案,并且每个节点将当前深度的计数+1,答案即为ma ...

  9. v4l2读取摄像头程序流程解析【转】

    转自:https://my.oschina.net/u/1024767/blog/210801 v4l2 操作实际上就是 open() 设备, close() 设备,以及中间过程的 ioctl() 操 ...

  10. zlib、libzip、 libzippp 库编译(windows + cmake + vs2013)

    "libzipp" 这库是基于 "libzip" 之上封装的,而 "libzip" 又是基于 "zlib"库封装的,所以 ...