FRIHOSTFORUMSFAQTOSBLOGSDIRECTORY
You are invited to Log in or Register a Frihost Account!

Javascript replace by array

 


Roald
Is it possible to use the replace() function in js with an array(like in PHP)?
Like this:
Code:
var replace=new array("...",...");
var by=new array(":::",:::");
var str="Here some text";
str.replace(replace,by);
alalex
not sure, try searching in google for all array functions... Sad
Roald
alalex wrote:
not sure, try searching in google for all array functions...
That's what I did but I didn't get the result I wanted, that's why I ask it here.
alalex
o sory, i didnt know. i never really worked wioth arrays in javascript, i cant help you... Sad
MrBlueSky
Roald wrote:
Is it possible to use the replace() function in js with an array(like in PHP)?
Like this:
Code:
var replace=new array("...",...");
var by=new array(":::",:::");
var str="Here some text";
str.replace(replace,by);


No. Use a loop

Code:


function girlify(str) {
  var replace = new array("man", "boy");
  var by = new array("woman", "girl");
  for (var i=0; i<replace.length; i++) {
     str = str.replace(replace, by);
  }
  return str;
}

Roald
Sorry but it didn't work. (and it was not the new Array because I tried that.)
MrBlueSky
Roald wrote:
Sorry but it didn't work. (and it was not the new Array because I tried that.)


Oops, sorry, my bad Embarassed

It should be:

Code:

function girlify(str) {
  var replace = new Array("man", "boy");
  var by = new Array("woman", "girl");
  for (var i=0; i<replace.length; i++) {
     str = str.replace(replace[i], by[i]);
  }
  return str;
}


I forgot the array-indices Rolling Eyes
Roald
Thanks I'm really stupid in Javascript. Because I have another problem: I'm generating a random number between 0 and 15 with this code:
Code:
var ranNum= Math.floor(Math.random()*15);
Well if I try to replace one of these randomly generated numbers by a letter it doesn't work. Can you help me with this?
MrBlueSky
Use String.fromCharCode()

for example:

Code:

var ranNum= Math.floor(Math.random()*15);
// ascii-code of 'a' is 97
alert(String.fromCharCode(97+ranNum));


Or do you mean something else?
Roald
A bit, I would like my script to generate every number between 0 and 15 but it has to replace the numbers 10 till 15 by their hexadecimal code (a,b,c,...)
MrBlueSky
Use this function to create random Hexidecimal numbers between 0 and F (inclusive):

Code:

<SCRIPT>

function randomHex () {
    var ranNum= Math.floor(Math.random()*16);
    var hexChar;
    if (ranNum<10) {
        hexChar = ""+ranNum;
    } else {
        hexChar = String.fromCharCode(55+ranNum);
    }
    return hexChar;
}

alert(randomHex());

</SCRIPT>


or use Javascripts Number.toString(radix) function to convert strings containing a decimal number to hexidecimal:

Code:

alert(Number(10).toString(16));
Roald
Now I've written to a function that can create hex numbers:
Code:
function random()
    {
        var ranNum = Math.floor(Math.random()*16);
        if(ranNum>=10){
                ranNum = String.fromCharCode(55+(ranNum-10));
            }
        return ranNum;
    }

I hope it's good, you've solved my problem now, thank you.
MrBlueSky
Remove the '-10':

Code:

function random()
    {
        var ranNum = Math.floor(Math.random()*16);
        if(ranNum>=10){
                ranNum = String.fromCharCode(55+(ranNum));
            }
        return ranNum;
    }
   
alert(random());


When I put in the 55 I already substracted 10: the ascii-code of A is 65 Smile
Roald
***** What am I stupid Evil or Very Mad
Reply to topic    Frihost Forum Index -> Scripting -> Html, CSS and Javascript

FRIHOST HOME | FAQ | TOS | ABOUT US | CONTACT US | SITE MAP
© 2005-2007 Frihost, forums powered by phpBB.