# Solved: javascript string replace



## firestormer (Sep 16, 2005)

im using this:

var s = new String;
s = location.href;
s.replace(/&/g,"£!");

but it isnt working, it should replace all the '&' in the address of the page in s such as "www.example.com/index.php?x=1&y=2&z=3"

becomes
"www.example.com/index.php?x=1£!y=2£!z=3"

Please help


----------



## tomdkat (May 6, 2006)

What's not working? What results do you get?

Peace...


----------



## MMJ (Oct 15, 2006)

& is probably a regex special character, you might need to escape it.

either way you don't need to use regexes tho.


```
var s = location.href.replace("&","£!");
```


----------



## firestormer (Sep 16, 2005)

The code isnt replacing the & at all, 

I cant use php as the document is a HTML and it is referenced about a zillion times, i need it in Javascript


----------



## firestormer (Sep 16, 2005)

!!!!! ooops

s = s.replace(/&/g,"@%");


----------



## tomdkat (May 6, 2006)

Ah, the replace() method just wasn't replacing the string "in place" but was returning a new string with the replaced characters. 

Thanks for the update! 

Peace...


----------



## MMJ (Oct 15, 2006)

firestormer said:


> The code isnt replacing the & at all,
> 
> I cant use php as the document is a HTML and it is referenced about a zillion times, i need it in Javascript


Yeah, I forgot the replace() doesn't work like php's str_replace:


```
var s = location.href.replace(/&/g,"@%");
```


----------

