Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 6 of 6
  1. #1
    New to the CF scene
    Join Date
    Jun 2009
    Posts
    3
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Search and replace through Array

    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.

  • #2
    Supreme Master coder! Old Pedant's Avatar
    Join Date
    Feb 2009
    Posts
    25,189
    Thanks
    75
    Thanked 4,341 Times in 4,307 Posts
    Code:
    <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.
    Last edited by Old Pedant; 06-29-2009 at 07:45 AM. Reason: added "var"

  • #3
    Supreme Master coder! Old Pedant's Avatar
    Join Date
    Feb 2009
    Posts
    25,189
    Thanks
    75
    Thanked 4,341 Times in 4,307 Posts
    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:
    Code:
    <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>

  • #4
    New Coder
    Join Date
    Jun 2009
    Posts
    81
    Thanks
    0
    Thanked 8 Times in 8 Posts
    This wil catch first occurance as Old Pedant explained above.
    Code:
    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.

    Code:
    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
    Last edited by rdspoons; 06-30-2009 at 12:21 AM.

  • #5
    New to the CF scene
    Join Date
    Jun 2009
    Posts
    3
    Thanks
    0
    Thanked 0 Times in 0 Posts
    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.

  • #6
    Supreme Master coder! Old Pedant's Avatar
    Join Date
    Feb 2009
    Posts
    25,189
    Thanks
    75
    Thanked 4,341 Times in 4,307 Posts
    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??


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •