Wrox Programmer Forums
Go Back   Wrox Programmer Forums > Web Programming > JavaScript > Javascript How-To
|
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 software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
 
Old January 18th, 2006, 12:06 PM
Friend of Wrox
 
Join Date: Jan 2005
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
 
Old January 18th, 2006, 12:33 PM
joefawcett's Avatar
Wrox Author
 
Join Date: Jun 2003
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)
 
Old January 18th, 2006, 12:53 PM
Friend of Wrox
 
Join Date: Jan 2005
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





Similar Threads
Thread Thread Starter Forum Replies Last Post
XSLT TRIM Function sagrock XSLT 1 November 23rd, 2007 08: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 01: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 06:03 AM





Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.