Go Back   CodingForums.com > :: Client side development > JavaScript programming

Before you post, read our: Rules & Posting Guidelines

Reply
 
Thread Tools Rate Thread
Enjoy an ad free experience by logging in. Not a member yet? Register.
Old 01-21-2013, 01:03 PM   PM User | #1
durangod
Senior Coder

 
Join Date: Nov 2010
Posts: 1,177
Thanks: 214
Thanked 31 Times in 30 Posts
durangod is on a distinguished road
How to properly use trim

This is what i have, is this correct?

if the client enters spaces after the input i want to trim it

Code:
//in this example the input is blue_ _ _   (_=spaces)

var inp = document.subregfrm.squest.value; 
var answ = inp.trim();

//answ should = blue   (no spaces)
Thanks
durangod is offline   Reply With Quote
Old 01-21-2013, 01:34 PM   PM User | #2
Philip M
Supreme Master coder!

 
Philip M's Avatar
 
Join Date: Jun 2002
Location: London, England
Posts: 17,044
Thanks: 197
Thanked 2,412 Times in 2,390 Posts
Philip M has a spectacular aura aboutPhilip M has a spectacular aura aboutPhilip M has a spectacular aura about
The trim() function is native to PHP (and other languages) but the Javascript inbuilt function String.trim() is implemented only in the most modern browsers. Prefer:-

Code:
var inp = "   abc   "
var answ = inp.replace(/^\s+|\s+$/g,"");  // the g switch is essential!!
or if you prefer something re-usable:-

Code:
function trim(str) {
return str.replace(/^\s+|\s+$/g,"");
}
Quizmaster: Since 1920 a statue of which former US President has resided in Parliament Square?
Contestant: Ronald Reagan
__________________

All the code given in this post has been tested and is intended to address the question asked.
Unless stated otherwise it is not just a demonstration.

Last edited by Philip M; 01-21-2013 at 01:45 PM..
Philip M is offline   Reply With Quote
Users who have thanked Philip M for this post:
durangod (01-21-2013)
Old 01-22-2013, 09:09 AM   PM User | #3
rnd me
Senior Coder

 
rnd me's Avatar
 
Join Date: Jun 2007
Location: Urbana
Posts: 3,469
Thanks: 9
Thanked 466 Times in 450 Posts
rnd me is a jewel in the roughrnd me is a jewel in the roughrnd me is a jewel in the rough
for performance, readability, and consistency, one should always use the native syntax.

Its actually been several years now that String.prototoype.trim() has been bundled with browsers.

if you need backwards-compatibility (aka graceful degradation) use a ployfill to make sure the String method is the same as it is in all current versions of all major browsers:

in the case of trim(), we can do it 100% backwards compatible, so there's no reason to have to balance an extra pair of parens when the proto is so handy:

Code:
"".trim|| (String.prototype.trim = function trim() {
  return this.replace(/^\s+|\s+$/g,"");
});

if you already hard-coded trim(str) into your code, you can speed up your flow by re-binding the native:


Code:
var trim=eval.call.bind("".trim);

//example usage:
trim( "  hello   ");
__________________
my site (updated 5/13)
STATS (2013/5) HTML5:90.2% MOB:14% IE7:0.5% IE8:8.6% IE9:9.8% IE10:10%
rnd me is offline   Reply With Quote
Old 01-22-2013, 11:19 AM   PM User | #4
Philip M
Supreme Master coder!

 
Philip M's Avatar
 
Join Date: Jun 2002
Location: London, England
Posts: 17,044
Thanks: 197
Thanked 2,412 Times in 2,390 Posts
Philip M has a spectacular aura aboutPhilip M has a spectacular aura aboutPhilip M has a spectacular aura about
String.prototoype.trim() is not implemented in IE<9. I see nothing wrong with the regex I suggested.
__________________

All the code given in this post has been tested and is intended to address the question asked.
Unless stated otherwise it is not just a demonstration.
Philip M is offline   Reply With Quote
Old 01-22-2013, 12:27 PM   PM User | #5
rnd me
Senior Coder

 
rnd me's Avatar
 
Join Date: Jun 2007
Location: Urbana
Posts: 3,469
Thanks: 9
Thanked 466 Times in 450 Posts
rnd me is a jewel in the roughrnd me is a jewel in the roughrnd me is a jewel in the rough
Quote:
Originally Posted by Philip M View Post
String.prototoype.trim() is not implemented in IE<9. I see nothing wrong with the regex I suggested.
right, which is why i used your regex and posted a polyfill version allowing you to use String.prototype.trim() in IE 5-8...

if you want to bench the two, you'll find the native version, which runs in C, executes dozens of times faster than a user regexp.

but above performance, its about coding style. why write custom code using a proprietary syntax when you can easily use the native syntax, even in 3-version old IE using the code i posted.

protos are easier to code because they are an after-thought, added to the right of a string expression, whereas functions have to wrap the expression. its much quicker to type [SHIFT]+[9][0] than two separate shift presses and some arrow keys, and protos separate content from presentation with a dot.
__________________
my site (updated 5/13)
STATS (2013/5) HTML5:90.2% MOB:14% IE7:0.5% IE8:8.6% IE9:9.8% IE10:10%
rnd me is offline   Reply With Quote
Reply

Bookmarks

Jump To Top of Thread


Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT +1. The time now is 12:25 AM.


Advertisement
Log in to turn off these ads.