[js高手之路] 跟GhostWu一起封装一个字符串工具库-扩展字符串位置方法(4)
本文,我们接着之前的框架继续扩展,这次扩展了一共有5个与字符串位置相关的方法
between( left, right )
返回两个字符串之间的内容, 如果第二个参数没有传递,返回的是找到的第一个参数 之后 到 字符串结尾的所有字符串
如果第二个参数传递了,但是从left这个位置查找不到,就返回空字符串
例子:
; (function (window, undefined) {
function init(obj, s) {
if (s !== null && s !== undefined) {
if (typeof s === 'string') {
obj.s = s;
} else {
obj.s = s.toString();
}
} else {
obj.s = s;
}
}
function G(s) {
init(this, s);
}
function GhostWu(s) {
return new G(s);
}
var sProto = String.prototype;
G.prototype = {
constructor: G,
capitalize: function () {
return new this.constructor(this.s.slice(0, 1).toUpperCase() + this.s.substring(1).toLowerCase());
},
trimLeft: function () {
var s;
if (sProto.trimLeft === 'undefined')
s = this.s.trimLeft();
else
s = this.s.replace(/^\s+/g, '');
return new this.constructor(s);
},
trimRight: function () {
var s;
if (sProto.trimRight === 'undefined')
s = this.s.trimRight();
else
s = this.s.replace(/\s+$/g, '');
return new this.constructor(s);
},
trim: function () {
var s;
if (typeof sProto.trim === 'undefined') {
s = this.s.replace(/^\s+|\s+$/g, '');
} else {
s = this.s.trim();
}
return new this.constructor(s);
},
camelize: function () {
var s = this.trim().s.replace(/(\-|_|\s)+(.)?/g, function (s0, s1, s2) {
return (s2 ? s2.toUpperCase() : '');
});
return new this.constructor(s);
},
dasherize: function () {
var s = this.trim().s.replace(/[_\s]+/g, '-').replace(/([A-Z])/g, '-$1').replace(/-+/g, '-').toLowerCase();
return new this.constructor(s);
},
between: function (left, right) {
var s = this.s;
var startPos = s.indexOf(left);
var endPos = s.indexOf(right, startPos + left.length);
if (endPos == -1 && right != null)
return new this.constructor('')
else if (endPos == -1 && right == null)
return new this.constructor(s.substring(startPos + left.length))
else
return new this.constructor(s.slice(startPos + left.length, endPos));
},
chompLeft: function (prefix) {
var s = this.s;
if (s.indexOf(prefix) === 0) {
s = s.slice(prefix.length);
return new this.constructor(s);
} else {
return this;
}
},
startWith: function () {
var prefixes = [].slice.call(arguments, 0);
if (this.s.indexOf(prefixes[0], 0) === 0) return true;
return false;
},
endWith: function () {
var prefixes = [].slice.call(arguments, 0),
pos = 0;
while (pos !== -1) {
if (this.s.indexOf(prefixes[0], pos) === (this.s.length - prefixes[0].length)) return true;
pos = this.s.indexOf(prefixes[0], pos + prefixes[0].length);
}
return false;
},
chompRight: function (suffix) {
if (this.endWith(suffix)) {
var s = this.s;
s = s.slice(0, s.length - suffix.length);
return new this.constructor(s);
} else {
return this;
}
}
};
window.G = GhostWu;
})(window, undefined);
[js高手之路] 跟GhostWu一起封装一个字符串工具库-扩展字符串位置方法(4)的更多相关文章
- [js高手之路] 跟GhostWu一起封装一个字符串工具库-架构篇(1)
所谓字符串工具库就是利用javascript面向对象的知识封装一个常用的字符串处理方法库,首先给这个库起个名字,好吧就叫ghostwu.js. 看下ghostwu.js的整体架构: ; (functi ...
- [js高手之路] 跟GhostWu一起封装一个字符串工具库-扩展trim,trimLeft,trimRight方法(2)
我们接着上一篇的继续,在上一篇我们完成了工具库的架构,本文扩展字符串去空格的方法, 一共有3个 1,trimLeft: 去除字符串左边的空格 2,trimRight: 去除字符串右边的空格 3,tri ...
- [js高手之路] 跟GhostWu一起封装一个字符串工具库-扩展camelize与dasherize方法(3)
在此之前,我们已经完成了4个方法: trimLeft, trimRight, trim, capitalize 本文,我们扩展驼峰式与下划线转化这两个对称的方法 camelize: 把空格,下划线,中 ...
- [js高手之路]设计模式系列课程-设计一个模块化扩展功能(define)和使用(use)库
模块化的诞生标志着javascript开发进入工业时代,近几年随着es6, require js( sea js ), node js崛起,特别是es6和node js自带模块加载功能,给大型程序开发 ...
- [js高手之路] html5 canvas教程 - 制作一个数码倒计时效果
效果图: 这个实例主要注意: 1,剩余时间的计算 2,每个时间数字的绘制 时间主要有0-9和一个冒号组成,用数组来表示( 0: 就是不画圆,1:就是画一个蓝色的圆 ) num.js文件: var di ...
- [js高手之路]封装运动框架实战左右与上下滑动的焦点轮播图
在这篇文章[js高手之路]打造通用的匀速运动框架中,封装了一个匀速运动框架,我们在这个框架的基础之上,加上缓冲运动效果,然后用运动框架来做幻灯片(上下,左右),效果如下: 1 2 3 4 5 // 0 ...
- [js高手之路]原型对象(prototype)与原型链相关属性与方法详解
一,instanceof: instanceof检测左侧的__proto__原型链上,是否存在右侧的prototype原型. 我在之前的两篇文章 [js高手之路]构造函数的基本特性与优缺点 [js高手 ...
- [js高手之路]设计模式系列课程-组合模式+寄生组合继承实战新闻列表
所谓组合模式,就是把一堆结构分解出来,组成在一起,现实中很多这样的例子,如: 1.肯德基套餐就是一种组合模式, 比如鸡腿堡套餐,一般是是由一个鸡腿堡,一包薯条,一杯可乐等组成的 2.组装的台式机同理, ...
- [js高手之路]Node.js实现简易的爬虫-抓取博客文章列表信息
抓取目标:就是我自己的博客:http://www.cnblogs.com/ghostwu/ 需要实现的功能: 抓取文章标题,超链接,文章摘要,发布时间 需要用到的库: node.js自带的http库 ...
随机推荐
- IIS 部署WCF服务注意事项
IIS部署WCF服务的时候经常会出现如下错误: System.ServiceModel.EndpointNotFoundException”类型的未经处理的异常在 WinformWcfHost.exe ...
- IT行业歧视40岁以上人群为找工作还要谎报年龄[转]
IT行业歧视40岁以上人群为找工作还要谎报年龄(这样不好) http://www.aliyun.com/zixun/content/2_6_616161.html [赛迪网讯]4月5日消息,许多40多 ...
- SAN & vSAN & vSAN storage
SAN (storage area network ) 定义: Storage area network (SAN) is a network that primarily connects the ...
- log4j日志重定向
配置相应的类名或者包名,将日志重新定向到输入文件里 log4j.rootLogger=INFO,DEBUG,CONSOLE ##过滤日志 log4j.logger.[类名|包名]=INFO,[输出目的 ...
- javascript数组(1) ——sort的工作原理及其他数组排序方法
一说到数组排序,最直观的想法就是用sort啊! 请问不用使用sort方法还可以使用什么方法进行数组排序? 比如 : 快速排序法.合并排序法.冒泡排序法.选择排序法.插入排序法.布尔排序法.交互排序. ...
- [分享] 自动化测试与持续集成方案-- UI 检查
对于自动化测试中,UI 自动化测试估计是最有争议的,让人又爱又恨. UI 自动化做回归测试,可以省下很多人力.如果版本一直不稳定,投入跟产出不成比例的. 时机 一般是要版本稳定,界面改动不大.如果迭代 ...
- (转)盒子概念和DiV布局
CSS盒子和DIV布局 (2013-11-24 16:17:29) 转载▼ 一.认识div层 1.<DIV>标记是一个区块容器标记,在标记之间可以放置其他一些HTML元素,例如p,h1,t ...
- 【SignalR学习系列】7. SignalR Hubs Api 详解(JavaScript 客户端)
SignalR 的 generated proxy 服务端 public class ContosoChatHub : Hub { public void NewContosoChatMessage( ...
- 初学 Python(十二)——高阶函数
初学 Python(十二)--高阶函数 初学 Python,主要整理一些学习到的知识点,这次是高阶函数. #-*- coding:utf-8 -*- ''''' 话说高阶函数: 能用函数作为参数的函数 ...
- 31. leetcode 122. Best Time to Buy and Sell Stock II
122. Best Time to Buy and Sell Stock II Say you have an array for which the ith element is the price ...