Wrox Programmer Forums

Need to download code?

View our list of code downloads.

Go Back   Wrox Programmer Forums > Web Programming > JavaScript > Javascript How-To
Password Reminder
Register
Register | FAQ | Members List | Calendar | Search | Today's Posts | Mark Forums Read
Javascript How-To Ask your "How do I do this with Javascript?" questions here.
Welcome to the p2p.wrox.com Forums.

You are currently viewing the Javascript How-To section of the Wrox Programmer to Programmer discussions. This is a community of tens of thousands of software programmers and website developers including Wrox book authors and readers. As a guest, you can read any forum posting. By joining today you can post your own programming questions, respond to other developersí questions, and eliminate the ads that are displayed to guests. Registration is fast, simple and absolutely free .
DRM-free e-books 300x50
Reply
 
Thread Tools Display Modes
  #1 (permalink)  
Old January 18th, 2006, 11:06 AM
Friend of Wrox
Points: 8,019, Level: 38
Points: 8,019, Level: 38 Points: 8,019, Level: 38 Points: 8,019, Level: 38
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: Jan 2005
Location: Mauchline, East Ayrshire, Scotland
Posts: 1,525
Thanks: 0
Thanked 0 Times in 0 Posts
Send a message via ICQ to crmpicco Send a message via AIM to crmpicco Send a message via MSN to crmpicco Send a message via Yahoo to crmpicco
Default custom JavaScript Trim function

I have the function 'Trim'.
However, I am experiancing errors as soon as it gets to
  return str.replace(/^\s*|\s*$/g,"");
Is there something wrong with this code?

Code:
/*
FUNCTION NAME: Trim
Removes any white space from either side of the string
*/
function Trim(str)
{
  return str.replace(/^\s*|\s*$/g,"");
}
Thanks,
Picco

www.crmpicco.co.uk
__________________
_______________________
Ayrshire Minis - a Mini E-Community
http://www.ayrshireminis.com
http://www.crmpicco.co.uk
Reply With Quote
  #2 (permalink)  
Old January 18th, 2006, 11:33 AM
joefawcett's Avatar
Wrox Author
Points: 9,763, Level: 42
Points: 9,763, Level: 42 Points: 9,763, Level: 42 Points: 9,763, Level: 42
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: Jun 2003
Location: Exeter, , United Kingdom.
Posts: 3,074
Thanks: 1
Thanked 38 Times in 37 Posts
Default

Where is this running? Some older browsers don't support regular expressions, especially with the // delimiters.

A more JavaScript way of doing this would be:
Code:
String.prototype.trim = function(){return this.replace(/^\s*|\s*$/g,"");}
alert("!" + "  my string with spaces   ".trim() + "!");
--

Joe (Microsoft MVP - XML)
Reply With Quote
  #3 (permalink)  
Old January 18th, 2006, 11:53 AM
Friend of Wrox
Points: 8,019, Level: 38
Points: 8,019, Level: 38 Points: 8,019, Level: 38 Points: 8,019, Level: 38
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: Jan 2005
Location: Mauchline, East Ayrshire, Scotland
Posts: 1,525
Thanks: 0
Thanked 0 Times in 0 Posts
Send a message via ICQ to crmpicco Send a message via AIM to crmpicco Send a message via MSN to crmpicco Send a message via Yahoo to crmpicco
Default

its running in an Classic ASP page with JavaScript. thats what i decided to do Joe, found that old prototype in my arhcive. thanks!

www.crmpicco.co.uk
Reply With Quote
Reply


Thread Tools
Display Modes

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 Off
HTML code is Off
Trackbacks are Off
Pingbacks are On
Refbacks are Off

Similar Threads
Thread Thread Starter Forum Replies Last Post
XSLT TRIM Function sagrock XSLT 1 November 23rd, 2007 07:39 AM
TRIM, ISTEXT function won't work in Excel VBA code munzer1 Excel VBA 1 May 29th, 2007 09:35 AM
trim function on page 206 jackhsu BOOK: Professional JavaScript for Web Developers ISBN: 978-0-7645-7908-0 1 December 19th, 2006 12:46 PM
javascript string Trim() frunction kokuthanda VBScript 0 October 12th, 2006 02:45 AM
How to call javascript function from VB function vinod_yadav1919 VB How-To 0 February 13th, 2006 05:03 AM



All times are GMT -4. The time now is 10:28 AM.


Powered by vBulletin®
Copyright ©2000 - 2019, Jelsoft Enterprises Ltd.
© 2013 John Wiley & Sons, Inc.