Jump to content

[SOLVED] Removing certain characters


rhyspaterson

Recommended Posts

Hey guys,

 

Have a bit of a problem, and am quite unsure of how to fix it. I have a variable that is returned to me that looks like this:

 

Policy=frank; IP Address=10.10.10.10

 

I want to strip the string and turn it into an array so that i have only 'frank' and '10.10.10.10' (array[0] and array[1] respectively).

 

Any suggestions my elite coders?

 

Cheers!  ;D

Link to comment
Share on other sites

Thanks for your reply!

 

Unfortunately i think i may have not been clear enough. I have a single variable with multiple bits of information within it.

 

var foo = 'Policy=frank; IP Address=10.10.10.10';

 

I would like to strip the foo variable of everything except 'frank' and '10.10.10.10' and split it into an array. Please note that 'frank' could be any name and '10.10.10.10' could be any IP address. I was thinking of something that would search for the = signs, capture everything after it until it reaches a space, then looked for the next = sign and do the same... but i have no idea how to go about it.

Link to comment
Share on other sites

<html>

    <script language="javascript">

        var str = "Policy=frank; IP Address=10.10.10.10";

 

        var mixed_array = new Array();

        var tmp_arr = new Array();

        var arr = new Array();

        mixed_array = str.split(';');

        for(x=0; x<mixed_array.length; x++) {

          tmp_arr = mixed_array[x].split('=');

          arr[x] = tmp_arr[1];

        }

 

        for(x=0; x<arr.length; x++) {

          document.write("array[" + x + "] -- " + arr[x]+"<br>");

        }

    </script>

</html>

 

Link to comment
Share on other sites

Thanks for that!

 

Turned it into a function and changed a few things:

 

/*
* Clean up the string
*/	
   splitStringToArray : function ( string ) {	

var string = dController.policy;
var mixedArray = new Array();
var temporaryArray = new Array();	
var array = new Array();	

mixedArray = string.split(';');

for(x = 0; x < mixedArray.length; x++) {
   temporaryArray = mixedArray[x].split('=');
   array[x] = temporaryArray[1];	   
   }
return array;
}
   ,

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.