本文,我们接着之前的框架继续扩展,这次扩展了一共有5个与字符串位置相关的方法

between( left, right )

返回两个字符串之间的内容, 如果第二个参数没有传递,返回的是找到的第一个参数 之后 到 字符串结尾的所有字符串

如果第二个参数传递了,但是从left这个位置查找不到,就返回空字符串

例子:

G( 'ghost wu tell you how to learn js' ).between( 'tell', 'learn' ).s   
返回的是tell和learn之间的字符串, 结果为:you how to
G( 'ghost wu tell you how to learn js' ).between( 'tell' ).s
返回的是tell后面所有的字符串,结果为:you how to learn js
G( 'ghost wu tell you how to learn js' ).between( 'tell', 'wu' ).s
返回空字符串
 
chompLeft( prefix )
例子:返回以prefix开头 到 字符串结尾的所有字符串
G( 'ghost wu tell you how to learn js' ).chompLeft( 'tell' ).s; //ghost wu tell you how to learn js
G( 'ghost wu tell you how to learn js' ).chompLeft( 'ghost' ).s; //wu tell you how to learn js
 
startWith( prefix ): 判断是否以prefix这个字符串开始
endWith( prefix ): 判断是否以prefixe这个字符串结尾
G( 'ghost wu tell you how to learn js---ghost wu' ).startWith('wu');//false
G( 'ghost wu!asbc# ghost wu' ).endWith('wu');//true
 
chompRight( prefix )
例子:返回从字符串开始到以prefix结尾之间的字符串
G( 'ghost wu tell you how to learn js---ghost wu' ).chompRight('wu').s;//ghost wu tell you how to learn js---ghost
 
 ; (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)的更多相关文章

  1. [js高手之路] 跟GhostWu一起封装一个字符串工具库-架构篇(1)

    所谓字符串工具库就是利用javascript面向对象的知识封装一个常用的字符串处理方法库,首先给这个库起个名字,好吧就叫ghostwu.js. 看下ghostwu.js的整体架构: ; (functi ...

  2. [js高手之路] 跟GhostWu一起封装一个字符串工具库-扩展trim,trimLeft,trimRight方法(2)

    我们接着上一篇的继续,在上一篇我们完成了工具库的架构,本文扩展字符串去空格的方法, 一共有3个 1,trimLeft: 去除字符串左边的空格 2,trimRight: 去除字符串右边的空格 3,tri ...

  3. [js高手之路] 跟GhostWu一起封装一个字符串工具库-扩展camelize与dasherize方法(3)

    在此之前,我们已经完成了4个方法: trimLeft, trimRight, trim, capitalize 本文,我们扩展驼峰式与下划线转化这两个对称的方法 camelize: 把空格,下划线,中 ...

  4. [js高手之路]设计模式系列课程-设计一个模块化扩展功能(define)和使用(use)库

    模块化的诞生标志着javascript开发进入工业时代,近几年随着es6, require js( sea js ), node js崛起,特别是es6和node js自带模块加载功能,给大型程序开发 ...

  5. [js高手之路] html5 canvas教程 - 制作一个数码倒计时效果

    效果图: 这个实例主要注意: 1,剩余时间的计算 2,每个时间数字的绘制 时间主要有0-9和一个冒号组成,用数组来表示( 0: 就是不画圆,1:就是画一个蓝色的圆 ) num.js文件: var di ...

  6. [js高手之路]封装运动框架实战左右与上下滑动的焦点轮播图

    在这篇文章[js高手之路]打造通用的匀速运动框架中,封装了一个匀速运动框架,我们在这个框架的基础之上,加上缓冲运动效果,然后用运动框架来做幻灯片(上下,左右),效果如下: 1 2 3 4 5 // 0 ...

  7. [js高手之路]原型对象(prototype)与原型链相关属性与方法详解

    一,instanceof: instanceof检测左侧的__proto__原型链上,是否存在右侧的prototype原型. 我在之前的两篇文章 [js高手之路]构造函数的基本特性与优缺点 [js高手 ...

  8. [js高手之路]设计模式系列课程-组合模式+寄生组合继承实战新闻列表

    所谓组合模式,就是把一堆结构分解出来,组成在一起,现实中很多这样的例子,如: 1.肯德基套餐就是一种组合模式, 比如鸡腿堡套餐,一般是是由一个鸡腿堡,一包薯条,一杯可乐等组成的 2.组装的台式机同理, ...

  9. [js高手之路]Node.js实现简易的爬虫-抓取博客文章列表信息

    抓取目标:就是我自己的博客:http://www.cnblogs.com/ghostwu/ 需要实现的功能: 抓取文章标题,超链接,文章摘要,发布时间 需要用到的库: node.js自带的http库 ...

随机推荐

  1. usaco 2002 月赛 Chores 题解

    Description Farmer John's family pitches in with the chores during milking, doing all the chores as ...

  2. 浅谈viewport

    我们通常在写移动端页面时,往往都会在html页面中加入这样一段话 <meta name="viewport" content="width=device-width ...

  3. Protobuf动态解析在Java中的应用 包含例子程序

    最近在做ProtoBuf相关的项目,其中用到了动态解析,网上看了下相关资料和博文都比较少,自己来写一个记录一下学习过程.   Protocol Buffers是结构化数据格式标准,提供序列化和反序列方 ...

  4. [luogu]P1352 没有上司的舞会[树形DP]

    本Lowbee第一次写树形DP啊,弱...一个变量写错半天没看出来...... 题目描述 某大学有N个职员,编号为1~N.他们之间有从属关系,也就是说他们的关系就像一棵以校长为根的树,父结点就是子结点 ...

  5. jsp函数的使用

    在这篇博客里面谈一谈jsp函数的一些使用规则 1.在jsp里面,函数和类是等价的,因为在函数的内部可以定义函数和变量.定义在函数内的函数和变量分为实例属性.实例函数.类属性.类函数.实例和类是面向对象 ...

  6. rabbitmq重装之后无法加入原有cluster的bug解析

    背景: 一台controller node,一台compute1节点 两台机器的host文件均已经进行hostname解析 两节点本已经加入了同一rabbitmq cluster 但controlle ...

  7. reversing.kr easy crack 之write up

    之前学逆向感觉学得一踏糊涂,这阶段好多师傅带我,一定要好好学,重新开始,认真学习. 来看打开可执行文件: 用ollydbg载入,单步执行后停到了入口点: 分析入口点,并没有加壳,于是F9执行程序,跳出 ...

  8. TeamCity : Build 失败条件

    允许用户配置 Build 失败的条件是很有用的功能,它是我们配置复杂 Build 流程的基础.TeamCity 为用户自定义 Build 失败条件提供了很好的支持.这些条件大体上可以分为两类,分别是: ...

  9. Uva 11988 Broken Keyboard STL+链表

    两种方法,直接上代码 STL标准模板库 #include <iostream> #include <list> #include <algorithm> #incl ...

  10. STextComboBox & SComboBox

    //简单智能 SNew(STextComboBox).ContentPadding(5).OptionsSource(&Options).InitiallySelectedItem(Optio ...