Click to See Complete Forum and Search --> : [js]Regexp problem


Tommienbp
04-18-2003, 06:52 AM
have a string called code and I want every foo.location.href to be removed from it.

I tried the following code, but that didn't work out. Anybody suggestions?

foo.location.href is something like: http://www.domain.com/admin/page.php?id_page=12 where id_page can be any number.

First attempt:
while (code.match(foo.location.href))
{
code = code.replace(foo.location.href,'');
}

Second attempt:
var url = new regExp(foo.location.href, "gi");
code = code.replace(url,'');

Third attempt:
var re2 = '/'+foo.location.href+'/gi';
code = code.replace(re2,'')

It didn't work out and all instances of foo.location.href kept in the code. What am I doing wrong?

pyro
04-18-2003, 08:10 AM
Ok, maybe I'm missing something, but what is foo.location.href? If it is the current URL, it should be document.location.href. Other than that, I'm not sure what you'd be returning. Anyway, try this code:

url = foo.location.href;
code = code.replace(url,"");

DrDaMour
04-18-2003, 12:08 PM
i don't think you can .replace(string,string) i think it has to be a regular expression. For a standard string the expression looks like below

<SCRIPT>re = /apples/gi;
str = "Apples are round, and apples are juicy.";
newstr=str.replace(re, "oranges");
document.write(newstr)
</SCRIPT>


so you should do

re = "/" + foo.location.href + "/gi";

and set re as the first parameter.

you can do it other ways i'm sure, but the / / i think aare necessary

Tommienbp
04-22-2003, 06:49 AM
Thanks that is what I was looking for. Even simpler than I thought. :D