Wrox Programmer Forums
|
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 June 10th, 2009, 10:43 AM
Friend of Wrox
 
Join Date: Oct 2003
Posts: 290
Thanks: 24
Thanked 0 Times in 0 Posts
Default SPACE REMOVAL WITH REG EXP

Hi,

This is removs the trailing and leading spaces in a string but not in the middle.

nTxt = nTxtObj.value.replace(/^\s+|\s+$/g,"")

example:

string = " RR55 JJ 12 "

becomes

string = "RR55 JJ 12"

What do I have to do to get rid of the spaces in the middle as well so that I get this:

string = "RR55JJ12"

Cheers

P


 
Old June 10th, 2009, 11:20 AM
joefawcett's Avatar
Wrox Author
 
Join Date: Jun 2003
Posts: 3,074
Thanks: 1
Thanked 38 Times in 37 Posts
Default

^ means "at the start" and $ means "at the end" so try a regex of:
Code:
/\s/g
__________________
Joe
http://joe.fawcett.name/

Last edited by joefawcett; June 10th, 2009 at 11:23 AM.. Reason: Improved expression by removing +: /\s+/ => /\s/
The Following User Says Thank You to joefawcett For This Useful Post:
pallone (June 10th, 2009)
 
Old June 10th, 2009, 02:08 PM
Friend of Wrox
 
Join Date: Oct 2003
Posts: 290
Thanks: 24
Thanked 0 Times in 0 Posts
Default

Hi Joe,

Thanks. That worked.

Cheers

P





Similar Threads
Thread Thread Starter Forum Replies Last Post
reg. exp. for up to 2 decimal places everest Javascript How-To 1 February 11th, 2008 04:51 PM
Visual Basic Exp 2005 Sorting 009221 Visual Basic 2005 Basics 1 July 2nd, 2007 03:02 PM
reg exp for password swarnap Javascript How-To 2 October 25th, 2006 02:04 AM
With Me .Show() I get error "end of statement exp kenn_rosie VS.NET 2002/2003 1 March 20th, 2006 09:12 PM
Sending email thru Outlook Exp. from VB with html rambhias Beginning VB 6 4 November 22nd, 2004 03:12 AM





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