kwilliams Posted April 3, 2007 Share Posted April 3, 2007 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: bababaVB version (works):'Declare variablesDim strA As String = "ZZ~"Dim strB As String = "YY~"Dim strValue_display As String = strValue_originalstrValue_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 (:" & strValue_display & "<br />")'Resulting value: YY~ZZ~YY~ZZ~YY~ZZ~JScript version (doesn't work)://Declare variablesvar 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 (:" + strValue_display + "<br />");//Resulting value: YY~ZZ~baba Link to comment Share on other sites More sharing options...
pulpfiction Posted April 3, 2007 Share Posted April 3, 2007 Try this... var strValue_original = "ababab"var strA = "ZZ~";var strB = "YY~";var strValue_display = strValue_original;strValue_display = strValue_display.replace(/a/g,strA);document.write("strValue_display (a):" + strValue_display + "<br />");strValue_display = strValue_display.replace(/b/g,strB);document.write("strValue_display (b):" + strValue_display + "<br />"); Link to comment Share on other sites More sharing options...
kwilliams Posted April 3, 2007 Author Share Posted April 3, 2007 Try this...var strValue_original = "ababab"var strA = "ZZ~";var strB = "YY~";var strValue_display = strValue_original;strValue_display = strValue_display.replace(/a/g,strA);document.write("strValue_display (a):" + strValue_display + "<br />");strValue_display = strValue_display.replace(/b/g,strB);document.write("strValue_display (b):" + strValue_display + "<br />"); It worked great! Thanks for the quick response. Link to comment Share on other sites More sharing options...
pulpfiction Posted April 3, 2007 Share Posted April 3, 2007 For more information about replace()....http://www.w3schools.com/jsref/jsref_replace.asp Link to comment Share on other sites More sharing options...
Recommended Posts
Create an account or sign in to comment
You need to be a member in order to leave a comment
Create an account
Sign up for a new account in our community. It's easy!
Register a new accountSign in
Already have an account? Sign in here.
Sign In Now