Preg_replace

Discussion in 'Programming' started by cbkman, Jan 27, 2007.

  1. #1
    Hey everyone, im wondering how I would go about doing the equivelant of preg_replace, in javascript

    basicallyt i wanna set something up so that if someone comes across the word "cat" on my site, it changes it to "kitty" or something along those lines.

    help please?:)
     
    cbkman, Jan 27, 2007 IP
  2. cbkman

    cbkman Peon

    Messages:
    31
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #2
    var str=document.body.innerHTML
    document.write(str.replace(/cat/, "kitty"))

    thats what ive tried so far...
    but it doesnt seem to be working
     
    cbkman, Jan 27, 2007 IP
  3. cbkman

    cbkman Peon

    Messages:
    31
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #3
    var str=document.body.whole
    document.write(str.replace(/cat/, "kitty"))

    i cant get this to work... ahhh
     
    cbkman, Jan 28, 2007 IP
  4. cbkman

    cbkman Peon

    Messages:
    31
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #4
    anyone care to reply and even say they DONT know what to do?

    aye yay aye
     
    cbkman, Jan 29, 2007 IP
  5. picouli

    picouli Peon

    Messages:
    760
    Likes Received:
    89
    Best Answers:
    0
    Trophy Points:
    0
    #5
    what do you mean "it doesn't work"? Gives you an error? (if you have Firefox, have you checked the Error Console?) Doesn't do anything?
    Also, can you please show more of your script and/or give us the URL - as your code seems fine to me...
     
    picouli, Jan 29, 2007 IP
  6. Barti1987

    Barti1987 Well-Known Member

    Messages:
    2,703
    Likes Received:
    115
    Best Answers:
    0
    Trophy Points:
    185