Advertisement
Guest User

Untitled

a guest
Oct 31st, 2014
177
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.14 KB | None | 0 0
  1. String.prototype.trim=function(){return this.replace(/^s+|s+$/g, '');};
  2.  
  3. String.prototype.ltrim=function(){return this.replace(/^s+/,'');};
  4.  
  5. String.prototype.rtrim=function(){return this.replace(/s+$/,'');};
  6.  
  7. String.prototype.fulltrim=function(){return this.replace(/(?:(?:^|n)s+|s+(?:$|n))/g,'').replace(/s+/g,' ');};
  8.  
  9. if (!String.prototype.trim) {
  10. //code for trim
  11. }
  12.  
  13. $.trim(' your string ');
  14.  
  15. if(!String.prototype.trim){
  16. String.prototype.trim = function(){
  17. return this.replace(/^s+|s+$/g,'');
  18. };
  19. }
  20.  
  21. String.prototype.trim = function() {
  22. return this.replace(/^s+|s+$/g, "");
  23. };
  24.  
  25. " foo bar ".trim(); // "foo bar"
  26.  
  27. function trim(str) {
  28. return str.replace(/^s+|s+$/g,"");
  29. }
  30.  
  31. document.getElementById("id").value.trim();
  32.  
  33. /**
  34. * Trim string. Actually trims all control characters.
  35. * Ignores fancy Unicode spaces. Forces to string.
  36. */
  37. function trim(str) {
  38. str = str.toString();
  39. var begin = 0;
  40. var end = str.length - 1;
  41. while (begin <= end && str.charCodeAt(begin) < 33) { ++begin; }
  42. while (end > begin && str.charCodeAt(end) < 33) { --end; }
  43. return str.substr(begin, end - begin + 1);
  44. }
  45.  
  46. var orig = " foo ";
  47. console.log( orig.trim() );//foo
  48.  
  49. ' Hello '.trim() //-> 'Hello'
  50.  
  51. ' Hello '.trimLeft() //-> 'Hello '
  52. ' Hello '.trimRight() //-> ' Hello'
  53.  
  54. if (!''.trimLeft) {
  55. String.prototype.trimLeft = function() {
  56. return this.replace(/^s+/,'');
  57. };
  58. String.prototype.trimRight = function() {
  59. return this.replace(/s+$/,'');
  60. };
  61. if (!''.trim) {
  62. String.prototype.trim = function() {
  63. return this.replace(/^s+|s+$/g, '');
  64. };
  65. }
  66. }
  67.  
  68. String.prototype.trim = String.prototype.trim || function () {
  69. return this.replace(/^s+|s+$/g, "");
  70. };
  71.  
  72. String.prototype.trimLeft = String.prototype.trimLeft || function () {
  73. return this.replace(/^s+/, "");
  74. };
  75.  
  76. String.prototype.trimRight = String.prototype.trimRight || function () {
  77. return this.replace(/s+$/, "");
  78. };
  79.  
  80. String.prototype.trimFull = String.prototype.trimFull || function () {
  81. return this.replace(/(?:(?:^|n)s+|s+(?:$|n))/g, "").replace(/s+/g, " ");
  82. };
  83.  
  84. var trim = (function() {
  85.  
  86. // if a reference is a `String`.
  87. function isString(value){
  88. return typeof value == 'string';
  89. }
  90.  
  91. // native trim is way faster: http://jsperf.com/angular-trim-test
  92. // but IE doesn't have it... :-(
  93. // TODO: we should move this into IE/ES5 polyfill
  94.  
  95. if (!String.prototype.trim) {
  96. return function(value) {
  97. return isString(value) ?
  98. value.replace(/^s*/, '').replace(/s*$/, '') : value;
  99. };
  100. }
  101.  
  102. return function(value) {
  103. return isString(value) ? value.trim() : value;
  104. };
  105.  
  106. })();
  107.  
  108. String.prototype.trim = String.prototype.trim || function(){ return jQuery.trim(this); };
  109.  
  110. var str = new String('my string');
  111. str= str.trim();
  112.  
  113. var str = " Hello World! ";
  114. alert(str.trim());
  115.  
  116. var some_string_with_extra_spaces=" goes here "
  117. console.log(some_string_with_extra_spaces.match(/S.*S|S/)[0])
  118.  
  119. console.log(some_string_with_extra_spaces.match(/S[sS]*S|S/)[0])
  120.  
  121. console.log(some_string_with_extra_spaces.match(/^s*(.*?)s*$/)[1])
  122.  
  123. var trim: (input: string) => string = String.prototype.trim
  124. ? ((input: string) : string => {
  125. return (input || "").trim();
  126. })
  127. : ((input: string) : string => {
  128. return (input || "").replace(/^s+|s+$/g,"");
  129. })
  130.  
  131. function trim(str)
  132. {
  133. var startpatt = /^s/;
  134. var endpatt = /s$/;
  135.  
  136. while(str.search(startpatt) == 0)
  137. str = str.substring(1, str.length);
  138.  
  139. while(str.search(endpatt) == str.length-1)
  140. str = str.substring(0, str.length-1);
  141.  
  142. return str;
  143. }
  144.  
  145. form.elements[i].value = trim(form.elements[i].value);
  146.  
  147. var illmatch= /^(s*)(?:.*?)(s*)$/
  148. function strip(me){
  149. var match= illmatch.exec(me)
  150. if(match && (match[1].length || match[2].length)){
  151. me= me.substring(match[1].length, p.length-match[2].length)
  152. }
  153. return me
  154. }
  155.  
  156. if(match && (match[1].length || match[3].length)){
  157. me= match[2]
  158. }
  159.  
  160. function trim(str){
  161. return str.split(' ').join();
  162. }
  163.  
  164. function trim(text) {
  165. return (text == null) ? '' : ''.trim.call(text);
  166. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement