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
| 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 Search this Thread Display Modes
  #1 (permalink)  
Old June 10th, 2009, 10:43 AM
Friend of Wrox
Points: 1,243, Level: 13
Points: 1,243, Level: 13 Points: 1,243, Level: 13 Points: 1,243, Level: 13
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: Oct 2003
Location: , , United Kingdom.
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


Reply With Quote
  #2 (permalink)  
Old June 10th, 2009, 11:20 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

^ 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/
Reply With Quote
The Following User Says Thank You to joefawcett For This Useful Post:
pallone (June 10th, 2009)
  #3 (permalink)  
Old June 10th, 2009, 02:08 PM
Friend of Wrox
Points: 1,243, Level: 13
Points: 1,243, Level: 13 Points: 1,243, Level: 13 Points: 1,243, Level: 13
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: Oct 2003
Location: , , United Kingdom.
Posts: 290
Thanks: 24
Thanked 0 Times in 0 Posts
Default

Hi Joe,

Thanks. That worked.

Cheers

P
Reply With Quote
Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search
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
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



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


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