1. Advertising
    y u no do it?

    Advertising (learn more)

    Advertise virtually anything here, with CPM banner ads, CPM email ads and CPC contextual links. You can target relevant areas of the site and show ads based on geographical location of the user if you wish.

    Starts at just $1 per CPM or $0.10 per CPC.

JS Replace Method Question...

Discussion in 'JavaScript' started by kuepper, Oct 17, 2009.

  1. #1
    This is my code:

    mytext = mytext.replace(/(\[\[(.+)\]\])/gi,"(<script src='http://www.mysite.com/myscript?module=mymodule\&myvar=$2\&Output=JS'>)");
    PHP:
    Despite escaping the &'s, the script doesn't replace the text [[ADSF]]. Instead the end result is just "(" without anything behind it.

    Does anyone know why this isn't working?

    Help would be enormously appreciated!
     
    kuepper, Oct 17, 2009 IP
  2. unigogo

    unigogo Peon

    Messages:
    286
    Likes Received:
    8
    Best Answers:
    0
    Trophy Points:
    0
    #2
    You script does return the result.

    [[ADSF]] is replaced to
    If you only want to replace the text in brackets, here is the code
     
    unigogo, Oct 18, 2009 IP
    kuepper and dimitar christoff like this.
  3. dimitar christoff

    dimitar christoff Active Member

    Messages:
    882
    Likes Received:
    62
    Best Answers:
    0
    Trophy Points:
    90
    #3
    nicely done, +rep (did not occur to me that the callback for .repalce could be an argumented relative function, that's brilliant!!! - makes total sense though)
     
    dimitar christoff, Oct 18, 2009 IP
  4. kuepper

    kuepper Peon

    Messages:
    80
    Likes Received:
    1
    Best Answers:
    0
    Trophy Points:
    0
    #4
    Thanks for the reply!

    And that is quite brilliant with regards to replacing the text within the brackets- I didn't realize you could do that either...

    However, I actually do want to replace the brackets and everything. The problem is that when I use the above code from my first post, it doesn't work.

    I can have the [[ASDF]] and it errors out showing just the opening "(" with nothing following it. But, when I hard code the same script code with "ASDF" instead of the "$2" it works just fine. Then, if I change the script code to a link "<a href="$2">$2</a>", for example, it correctly creates the link.

    This leads me to believe that the $2 is working, the script is working, but the <script> created by the replace function isn't working for some reason (even though it should). Do you guys see any reason why this wouldn't be working? Escaped characters or anything that I'm missing?

    Thanks again!
     
    kuepper, Oct 18, 2009 IP
  5. kuepper

    kuepper Peon

    Messages:
    80
    Likes Received:
    1
    Best Answers:
    0
    Trophy Points:
    0
    #5
    Upon further investigation, the "&" is being replaced by "&amp;" and is causing the problem. I tried escaping it with "\", using "&amp;" in the code, using "%26" in the code, using "\u206" in the code, using CDATA, placing it in an external javascript, and all to no avail. Does anyone know how to keep the "&" in the URL in this case???
     
    kuepper, Oct 18, 2009 IP