I am trying to use the replace statement in javascript so that ultimately, i can create an array out of some data that is currently passed in a string.
I have the following javascript:
console.log('data from server:' + server_rule_segements);
//remove trailing ~
server_rule_segements = server_rule_segements.substring(0,server_rule_segements.length-2); // stripping off trailing ~,
console.log("1 - " + server_rule_segements);
server_rule_segements = server_rule_segements.replace("~,,", "~");
console.log("2 - " + server_rule_segements);
Here's the results in the console:
data from server:Home Number,1234,1,no~,,Work Number,12342342,1,no~,,Work Number,12344412341234,1,no~,
1 - Home Number,1234,1,no~,,Work Number,12342342,1,no~,,Work Number,12344412341234,1,no
2 - Home Number,1234,1,no~Work Number,12342342,1,no~,,Work Number,12344412341234,1,no
What I'm wondering is why the replace command didn't replace all the instances of "~,,". As you can see in the 2nd debug statement, there's still one there.. in what I'm calling "record 2". I'm sure it's something simple that I've missed... but I can't see it right now.
As I test, I changed the code so that I call the replace method twice, like so:
server_rule_segements = server_rule_segements.replace("~,,", "~");
server_rule_segements = server_rule_segements.replace("~,,", "~");
and then it works. But I don't think I should have to do that.
replace("\~\,\,","\~")and see if that works.replaceonly replaces all occurrences if the first argument is a regexp with thegmodifier. If it's a string, it just does one replacement.