...

View Full Version : Search and replace through Array



ag201
06-29-2009, 07:27 AM
hi guys,

i have a implementation where i need to perform a search and replace. i will try to explain to the best of my ability.

i have a variable,
var a = "THIS IS SOME SOME TEXT";

then i have another array variable,
var b = new Array();
which contains elements in key=value format eg. SOME=NEWSOME

so what i need to do is, i need to loop through this array and if SOME exists in var a, then i need to replace it with NEWSOME. so for each value before =, if it exists in var a replace it with whats after =. and replace only once, so if SOME exists multiple times just replace the first occurence.

so essentially my final string would be, THIS IS NEWSOME SOME TEXT.

can somebody please help me and tell me how can i go about this.

thanks in advance.

Old Pedant
06-29-2009, 07:42 AM
<script>
var text = "THIS IS SOME SOME TEXT";

var changes = { "SOME" : "NEWSOME", "IS" : "AT" };

alert( "before: " + text );

for ( var change in changes )
{
text = text.replace( change, changes[change] );
}
alert( "after: " + text );

</script>

Notice that I did not use your "SOME=NEWSOME" form in the array but instead used a natural object array. Simpler and easier to then code the for loop.

Note that string.replace( ) in javascript *DOES* only replace the first occurrence; you must use a regular expression and specify the "g"(lobal) option to replace all occurrences. So what you wanted was actually the easiest possible option.

Old Pedant
06-29-2009, 07:45 AM
If you really NEEDED to use the "NEWSOME=SOME" format (perhaps because you are getting the list of paired words from some outside source?), then you could do it thus:


<script>
var text = "THIS IS SOME SOME TEXT";

var changes = ["SOME=NEWSOME", "IS=AT" ];

alert( "before: " + text );

for ( var c = 0; c < changes.length; ++c )
{
var pair = changes[c].split("=");
text = text.replace( pair[0], pair[1] );
}
alert( "after: " + text );

</script>

rdspoons
06-30-2009, 12:16 AM
This wil catch first occurance as Old Pedant explained above.


var a = "THIS IS SOME SOME TEXT";
var b=new Array();
b["SOME"]="NEWSOME";

for(var i in b)
a=a.replace(i,b[i]);

alert(a);

result is "THIS IS NEWSOME SOME TEXT"

This wil catch all occurances as Old Pedant explained above.



var a = "THIS IS SOME SOME TEXT";
var b=new Array();
b["SOME"]="NEWSOME";

var r;
for(var i in b){
r=new RegExp(i,"g");
a=a.replace(r,b[i]);
}

alert(a);

result is "THIS IS NEWSOME NEWSOME TEXT"

tested in ie7, ff3

ag201
06-30-2009, 01:00 AM
Old Pedant your code replaces it first time and then its fine. But if my key value pairs are multiple, then only the last values are actually replaced, as everytime it loops it over writes the replaced text.

please help.

Old Pedant
06-30-2009, 02:44 AM
In the code I gave you, the actual alert( )s that I saw were (without the coloring, of course):


before: THIS IS SOME SOME TEXT

after: THAT IS NEWSOME SOME TEXT

In each case, only the first occurrence of the text on the left side of the equals sign was replaced.

So I have no idea why your code replaces (for example) "TEXT" more than one time.

How about showing us your ACTUAL code??



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum