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 April 3rd, 2007, 11:13 AM
Banned
Points: 1,561, Level: 16
Points: 1,561, Level: 16 Points: 1,561, Level: 16 Points: 1,561, Level: 16
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: Jul 2005
Location: , , .
Posts: 317
Thanks: 0
Thanked 0 Times in 0 Posts
Default Replace Method in JavaScript

I'm trying to use the replace method to replace all instances of "a" and "b" characters within a string, but I'm running into a problem. I have accomplished this in my VB version without a problem, but the JavaScript version is only replacing the first instance of the character in the string. I've included all of the referenced code below. If anyone could please let me know what I'm doing wrong, that would be great. Thanks for any and all help.

Problem: JS version replaces only the first instance of a character, while the VB version replaces all instances of characters.

Example value: bababa

VB version (works):
'Declare variables
Dim strA As String = "ZZ~"
Dim strB As String = "YY~"
Dim strValue_display As String = strValue_original
strValue_display = strValue_display.replace("a",strA)
Response.Write("strValue_display (a):" & strValue_display & "<br />")
strValue_display = strValue_display.replace("b",strB)
Response.Write("strValue_display (b):" & strValue_display & "<br />")
'Resulting value: YY~ZZ~YY~ZZ~YY~ZZ~

JScript version (doesn't work):
//Declare variables
var strA = "ZZ~";
var strB = "YY~";
var strValue_display = strValue_original;
strValue_display = strValue_display.replace("a",strA);
Response.Write("strValue_display (a):" + strValue_display + "<br />");
strValue_display = strValue_display.replace("b",strB);
Response.Write("strValue_display (b):" + strValue_display + "<br />");
//Resulting value: YY~ZZ~baba

KWilliams
Reply With Quote
  #2 (permalink)  
Old April 3rd, 2007, 11:22 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

replace takes a regular expression:
Code:
var s = "Joe Fawcett";
var oRE = /e/g; //g flag for global replace, i for case insensitive
var sNew = s.replace(oRE, x);
//sNew = "Jox Fawcxtt"
--

Joe (Microsoft MVP - XML)
Reply With Quote
  #3 (permalink)  
Old April 3rd, 2007, 12:50 PM
Banned
Points: 1,561, Level: 16
Points: 1,561, Level: 16 Points: 1,561, Level: 16 Points: 1,561, Level: 16
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: Jul 2005
Location: , , .
Posts: 317
Thanks: 0
Thanked 0 Times in 0 Posts
Default

It worked great! Thanks for the quick response.

KWilliams
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
replace css with javascript Maxxim CSS Cascading Style Sheets 11 October 7th, 2007 02:29 PM
Replace method don't work with [ ] / skyler Javascript How-To 1 December 30th, 2006 10:04 AM
Javascript to replace auto refresh ? Mantis Java Databases 0 October 22nd, 2005 06:22 AM
String.replace() method JohnD Javascript 0 August 20th, 2004 06:46 AM
Javascript SET Method? TSEROOGY Javascript How-To 2 February 7th, 2004 09:20 PM



All times are GMT -4. The time now is 05:02 AM.


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