本文,我们接着之前的框架继续扩展,这次扩展了一共有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. AngularJs学习笔记2-控制器、数据绑定、作用域

    上次分享完该系列文章后有朋友也建议说1.x版本除了维护也没有必要学习,可以学习2.0开始学习,我也知道1.x无论是从性能还是架构上都没有2.x好,但是我想因为现在也有一些朋友还在用1.x版本,因为1. ...

  2. Chrome浏览器扩展开发系列之十二:Content Scripts

    Content Scripts是运行在Web页面的上下文的JavaScript文件.通过标准的DOM,Content Scripts 可以操作(读取并修改)浏览器当前访问的Web页面的内容. Cont ...

  3. Html5元素布局

    本教程十分简单,适合新手(因为我也是新手).本教程参考了"菜鸟教程". 笔者希望做到元素相对于浏览器的角落布局,即九个典型位置: 这个理念其实和UE4中的UMG锚定一样.Html5 ...

  4. Java并发编程深入学习

    上周的面试中,被问及了几个并发开发的问题,自己回答的都不是很系统和全面,可以说是"头皮发麻",哈哈.因此果断购入<Java并发编程的艺术>一书,该书内容主要是对ifev ...

  5. 读Zepto源码之Callbacks模块

    Callbacks 模块并不是必备的模块,其作用是管理回调函数,为 Defferred 模块提供支持,Defferred 模块又为 Ajax 模块的 promise 风格提供支持,接下来很快就会分析到 ...

  6. nyoj_2:括号配对问题

    模拟栈的操作,很基础的一道题 题目链接: http://acm.nyist.net/JudgeOnline/problem.php?pid=2 #include<stdio.h> #inc ...

  7. STM32使用cube生成的程序后在keil5编译后首次SWD可以下载再次下载不行的解决办法。

    使用cube配置导出工程在keil5编译后首次SWD下载可以再次下载不行的解决办法. 1原因: cube使用的是HAL库,初始化语句里面禁用了调试功能. 在stm32f1xx_hal_msp.c中 _ ...

  8. ML(2)--感知机

    案例银行办信用卡--获得感知机 我们到银行办信用卡时,银行并不是直接就给你办卡的,而是会根据你的一些个人信息.消费信息.个人信誉等指标综合考虑后,才会决定是否给你办卡(不像现在银行办信用卡有点随意). ...

  9. Orchard 学习

    https://github.com/OrchardCMS/Orchard  源码下载 http://www.orchardch.com/  中文介绍网站

  10. [PS相关]DAS,NAS,SAN三种存储技术比较

    随着数据量一直在快速增长,存储技术也在快速的更新以满足需求和推动创新.当存储被提到的时候,它不仅仅局限于存储容量,还有其他的需求比如数据保护,数据备份,数据访问速度等等. NAS-网络存储设备(Net ...