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 April 3rd, 2007, 11:13 AM
Join Date: Jul 2005
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

Old April 3rd, 2007, 11:22 AM
joefawcett's Avatar
Wrox Author
Join Date: Jun 2003
Posts: 3,074
Thanks: 1
Thanked 38 Times in 37 Posts

replace takes a regular expression:
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)
Old April 3rd, 2007, 12:50 PM
Join Date: Jul 2005
Posts: 317
Thanks: 0
Thanked 0 Times in 0 Posts

It worked great! Thanks for the quick response.


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

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