本文,我們接著之前的框架繼續擴展,這次擴展了一共有5個與字元串位置相關的方法 between( left, right ) 返回兩個字元串之間的內容, 如果第二個參數沒有傳遞,返回的是找到的第一個參數 之後 到 字元串結尾的所有字元串 如果第二個參數傳遞了,但是從left這個位置查找不到,就返回空字 ...
本文,我們接著之前的框架繼續擴展,這次擴展了一共有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---ghost1 ; (function (window, undefined) { 2 function init(obj, s) { 3 if (s !== null && s !== undefined) { 4 if (typeof s === 'string') { 5 obj.s = s; 6 } else { 7 obj.s = s.toString(); 8 } 9 } else { 10 obj.s = s; 11 } 12 } 13 14 function G(s) { 15 init(this, s); 16 } 17 18 function GhostWu(s) { 19 return new G(s); 20 } 21 22 var sProto = String.prototype; 23 G.prototype = { 24 constructor: G, 25 capitalize: function () { 26 return new this.constructor(this.s.slice(0, 1).toUpperCase() + this.s.substring(1).toLowerCase()); 27 }, 28 trimLeft: function () { 29 var s; 30 if (sProto.trimLeft === 'undefined') 31 s = this.s.trimLeft(); 32 else 33 s = this.s.replace(/^\s+/g, ''); 34 return new this.constructor(s); 35 }, 36 trimRight: function () { 37 var s; 38 if (sProto.trimRight === 'undefined') 39 s = this.s.trimRight(); 40 else 41 s = this.s.replace(/\s+$/g, ''); 42 return new this.constructor(s); 43 }, 44 trim: function () { 45 var s; 46 if (typeof sProto.trim === 'undefined') { 47 s = this.s.replace(/^\s+|\s+$/g, ''); 48 } else { 49 s = this.s.trim(); 50 } 51 return new this.constructor(s); 52 }, 53 camelize: function () { 54 var s = this.trim().s.replace(/(\-|_|\s)+(.)?/g, function (s0, s1, s2) { 55 return (s2 ? s2.toUpperCase() : ''); 56 }); 57 return new this.constructor(s); 58 }, 59 dasherize: function () { 60 var s = this.trim().s.replace(/[_\s]+/g, '-').replace(/([A-Z])/g, '-$1').replace(/-+/g, '-').toLowerCase(); 61 return new this.constructor(s); 62 }, 63 between: function (left, right) { 64 var s = this.s; 65 var startPos = s.indexOf(left); 66 var endPos = s.indexOf(right, startPos + left.length); 67 if (endPos == -1 && right != null) 68 return new this.constructor('') 69 else if (endPos == -1 && right == null) 70 return new this.constructor(s.substring(startPos + left.length)) 71 else 72 return new this.constructor(s.slice(startPos + left.length, endPos)); 73 }, 74 chompLeft: function (prefix) { 75 var s = this.s; 76 if (s.indexOf(prefix) === 0) { 77 s = s.slice(prefix.length); 78 return new this.constructor(s); 79 } else { 80 return this; 81 } 82 }, 83 startWith: function () { 84 var prefixes = [].slice.call(arguments, 0); 85 if (this.s.indexOf(prefixes[0], 0) === 0) return true; 86 return false; 87 }, 88 endWith: function () { 89 var prefixes = [].slice.call(arguments, 0), 90 pos = 0; 91 while (pos !== -1) { 92 if (this.s.indexOf(prefixes[0], pos) === (this.s.length - prefixes[0].length)) return true; 93 pos = this.s.indexOf(prefixes[0], pos + prefixes[0].length); 94 } 95 return false; 96 }, 97 chompRight: function (suffix) { 98 if (this.endWith(suffix)) { 99 var s = this.s; 100 s = s.slice(0, s.length - suffix.length); 101 return new this.constructor(s); 102 } else { 103 return this; 104 } 105 } 106 }; 107 108 window.G = GhostWu; 109 })(window, undefined);