Welcome, guest | Sign In | My Account | Store | Cart

I do not like jQuery that's why I wrote own implementation for trimming strings. There are two ways how to do it.

JavaScript, 29 lines
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
//anonymous function
(function() {
  this.trim  = function($) { return $.replace(/^\s+|\s+$/g, ''); };
  this.lTrim = function($) { return $.replace(/^\s+/, ''); };
  this.rTrim = function($) { return $.replace(/\s+$/, ''); };
})();

/*
alert("\"" + trim(" test string ") + "\"");
alert("\"" + lTrim(" test string ") + "\"");
alert("\"" + rTrim(" test string ") + "\"");
*/

//prototypes
String.prototype.trim  = function() {
  return this.replace(/^\s+|\s+$/g, '');
}
String.prototype.lTrim = function() {
  return this.replace(/^\s+/, '');
}
String.prototype.rTrim = function() {
  return this.replace(/\s+$/, '');
}

/*
alert("\"" + " test string ".trim() + "\"");
alert("\"" + " test string ".lTrim() + "\"");
alert("\"" + " test string ".rTrim() + "\"");
*/
Created by greg zakharov on Tue, 9 Jul 2013 (MIT)
JavaScript recipes (69)
greg zakharov's recipes (59)

Required Modules

  • (none specified)

Other Information and Tasks