JavaScript Development
 
Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
User Name:
Password:
Remember me
Go Back   Dev Shed ForumsWeb DesignJavaScript Development

Reply
Add This Thread To:
  Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb 
Thread Tools Search this Thread Rate Thread Display Modes
 
Unread Dev Shed Forums Sponsor:
  #1  
Old July 2nd, 2003, 11:08 AM
Matthew Doucette Matthew Doucette is offline
matthewdoucette.com
Dev Shed Novice (500 - 999 posts)
 
Join Date: May 2002
Posts: 635 Matthew Doucette User rank is Private First Class (20 - 50 Reputation Level)Matthew Doucette User rank is Private First Class (20 - 50 Reputation Level) 
Time spent in forums: 10 h 8 m 16 sec
Reputation Power: 7
javascript string manipulation (modifying chars inside a string)

This is simple, but I just forget and it's hard to search it... How do you modify a chars inside a string? For example, convert "_" to " " (spaces.)
__________________
Matthew Doucette / Xona.com

Reply With Quote
  #2  
Old July 2nd, 2003, 12:16 PM
magma's Avatar
magma magma is offline
Contributing User
Dev Shed Novice (500 - 999 posts)
 
Join Date: Apr 2003
Location: Italy, EU
Posts: 517 magma User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 11 h 45 m 8 sec
Reputation Power: 6
You can use String.replace(pattern,string)
__________________
Cheers, Dave

Reply With Quote
  #3  
Old July 2nd, 2003, 03:43 PM
Matthew Doucette Matthew Doucette is offline
matthewdoucette.com
Dev Shed Novice (500 - 999 posts)
 
Join Date: May 2002
Posts: 635 Matthew Doucette User rank is Private First Class (20 - 50 Reputation Level)Matthew Doucette User rank is Private First Class (20 - 50 Reputation Level) 
Time spent in forums: 10 h 8 m 16 sec
Reputation Power: 7
Thanks, that worked. Here's the code:

"originalstr" is an array of strings. "modifiedstr" is the modified string, modified once for each element in the "originalstr" array. The code:

re=RegExp('_','gi');
modifiedstr=originalstr[i].replace(re,' ');

Reply With Quote
  #4  
Old September 9th, 2003, 07:19 AM
Birdy Birdy is offline
Junior Member
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Sep 2003
Posts: 1 Birdy User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 0
For me, the code did not work. But this one did:

re = new RegExp('_','gi');
modifiedstr = originalstr[i].replace(re,' ');

To make it complete, the following replace function will replace any string by any string inside a string:
Code:
<html>
  <head>
    <script>
      function replace(myString, toReplace, replacedBy) {
        return(myString.replace(new RegExp(toReplace, 'gi'), replacedBy));
      }
    </script>
  </head>
  <body onLoad="alert(replace('blablabla', 'bl', 'c'));">
  </body>
</html>

Last edited by Birdy : September 9th, 2003 at 07:36 AM.

Reply With Quote
  #5  
Old September 9th, 2003, 09:15 AM
Matthew Doucette Matthew Doucette is offline
matthewdoucette.com
Dev Shed Novice (500 - 999 posts)
 
Join Date: May 2002
Posts: 635 Matthew Doucette User rank is Private First Class (20 - 50 Reputation Level)Matthew Doucette User rank is Private First Class (20 - 50 Reputation Level) 
Time spent in forums: 10 h 8 m 16 sec
Reputation Power: 7
What browser are you using? Look like you had to add "new" in front of "RegExp"... why is that? Is it possible my code is not working in some browsers?!

Last edited by Doucette : September 9th, 2003 at 09:23 AM.

Reply With Quote
Reply

Viewing: Dev Shed ForumsWeb DesignJavaScript Development > javascript string manipulation (modifying chars inside a string)


Thread Tools  Search this Thread 
Search this Thread:

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

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
View Your Warnings | New Posts | Latest News | Latest Threads | Shoutbox
Forum Jump


Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
  
 





© 2003-2008 by Developer Shed. All rights reserved. DS Cluster 4 hosted by Hostway
Stay green...Green IT