issues getting and regex'ing a url

D

daveyand

Hey guys,

I've spent abit of time searching th web and also this group and cant
seem to find a solution to my issue.

Basically i want to get the current url, and then replace http:// with
something else.

Here is the current code.


var current_url = window.document.location;
var re = new RegExp("http://", "g");
if(re.test(current_url)) {

me = current_url.replace(re,"http://www.addme.com/");

window.alert("found :: " + me + " :: " + current_url);
} else {
window.alert("not");
}

if my page was http://ww.google.com 'd get the alert to be:

found :: undefined :: http://www.google.com.

I dont understand why i am getting undefined. When re.test() works.
surely that means the regex is correct. I've used the same code in:
http://www.regular-expressions.info/javascriptexample.html .

Cheers for any help guys.
 
L

Lee

daveyand said:
Hey guys,

I've spent abit of time searching th web and also this group and cant
seem to find a solution to my issue.

Basically i want to get the current url, and then replace http:// with
something else.

Here is the current code.


var current_url = window.document.location;
var re = new RegExp("http://", "g");
if(re.test(current_url)) {

me = current_url.replace(re,"http://www.addme.com/");

window.alert("found :: " + me + " :: " + current_url);
} else {
window.alert("not");
}

if my page was http://ww.google.com 'd get the alert to be:

found :: undefined :: http://www.google.com.

I dont understand why i am getting undefined. When re.test() works.
surely that means the regex is correct. I've used the same code in:
http://www.regular-expressions.info/javascriptexample.html .

What's the point of the "g" ?
That's not causing problems, though.
The following works for me in IE and Firefox.
The only change I've made is to assign the value of current_url:

<html>
<body>
<script type="text/javascript">
var current_url = "http://www.google.com";
var re = new RegExp("http://", "g");
if(re.test(current_url)) {

me = current_url.replace(re,"http://www.addme.com/");

window.alert("found :: " + me + " :: " + current_url);
} else {
window.alert("not");
}
</script>
</body>
</html>
 
V

VK

daveyand said:
Hey guys,

I've spent abit of time searching th web and also this group and cant
seem to find a solution to my issue.

Basically i want to get the current url, and then replace http:// with
something else.

What is the value of manuals if no one bothers to read them? :))

alert(window.location.protocol);
// gives you "file:" or "http:" or
// wherever you are.

window.location.protocol = "https:";
// view the current page over SSL
 
D

daveyand

hey VK,

Not so sure what you're telling me here, doesnt seem to help my issue
at all. But that is something thats quite useful and might be able to
help me somewhere else in the future.

Lee,

The point of the g, is just test, i couldnt get it to work so i added g
have also tried with i. Its not needed i know that :)

As for your solution, its a weird one, i mean it works. But obviously
i want this to be dynamic, (maybe i should have stated that ???). I've
also tried this, but again doesnt work:

if(re.test(window.content.location)) {

var me = window.content.location.replace(re,"http://www.addme.com/");
window.alert("found :: " + me);
} else {
window.alert("not");
}

And once again we get the same thing, me just isnt defined. Although
by rights it should be. Seeing as we get into the logicblock.

But i can see you got it working, i'm just trying to see whats up. Do
i need to chomp or trim the JS like i might have to in perl?
 
D

daveyand

doesnt seem to work :(

As for what content is thats a firefox thing. This regex will be used
in a firefox extension. If i just use location i get a chrome:// uri.

Make sense?
 
E

Evertjan.

daveyand wrote on 09 jan 2006 in comp.lang.javascript:
doesnt seem to work :(

What does not?
As for what content is thats a firefox thing. This regex will be used
in a firefox extension. If i just use location i get a chrome:// uri.

Make sense?

No.

If you do not quote on usenet, we won't know what you are talking about.
 
V

VK

daveyand said:
hey VK,

Not so sure what you're telling me here

I'm telling you that window.location / document.location are
pre-divided onto all needed segments - you just need to ask for the
right one(s).

I presumed (obviously wrongly) what you wanted to switch the current
protocol, for that you need window.location.protocol = 'whatever';

If you need just to store whatever follows the protocol (w/o slashes)
like from "http://www.myserver.com/mypage.html" you want to have
"www.myserver.com/mypage.html" then in HTTP it's called *pathname* and
the relevant string is
document.location.pathname

There are also hash, host, hostname, port, protocol and search for all
possible HTTP URL components.
Studying location.href through RegExp is also possible but has no
practical sense (unless for practice itself).

IMHO
 
V

VK

daveyand said:
ah so VK,

i could do something like this.

current_site = window.location.pathname;

new_url = "http://www.addme.com/" + current_site;
window.location = new_url;

???

Yes you can. You also can (current_site + 1000) : it all depends on
your objectives :)

The OP question was:
Basically i want to get the current url,
and then replace http:// with something else.

This question is already answered:
var something = myPart + document.location.pathname;

If it doesn't do exactly what you want then maybe the OP question was
not full enough?
 
R

RobG

daveyand said:
ah so VK,

i could do something like this.

current_site = window.location.pathname;

new_url = "http://www.addme.com/" + current_site;
window.location = new_url;

???

To see the properties and values of the location object for any page, copy
and paste the following as a single line into the address bar and hit enter
(you can save it as a bookmarklet):

javascript:x=location;t='';for(var p in x){t+=p+': '+x[p]+'\n';}alert(t);


Works in Firefox & IE but not Safari 1.0.3 (it may work in later versions).
 
D

daveyand

na it works. I've not got this:

var hostname = window.content.location.hostname;
var path = window.content.location.pathname;

url = "http://www.addme.com/" + hostname + path;

So much easier, and such little hassle. :)

no all i need to do is regex the hostname. But only good at perl ones.
Want to do this: (using perl syntax here hopefully you'll understand)

(country) = (hostname =~ m/(uk|es|fr|de).(.*?).mysite.com/);

so this would match something like: uk.blah.mysite.com

or ultimatly i want to set $1 to (uk|es|fr|de). Like i;ve said this is
perl code, and will look onine but if you can help then that would be
grand :)
 
L

Lee

daveyand said:
hey VK,

Not so sure what you're telling me here, doesnt seem to help my issue
at all. But that is something thats quite useful and might be able to
help me somewhere else in the future.

Lee,

The point of the g, is just test, i couldnt get it to work so i added g
have also tried with i. Its not needed i know that :)

As for your solution, its a weird one, i mean it works. But obviously
i want this to be dynamic, (maybe i should have stated that ???). I've
also tried this, but again doesnt work:

I didn't intend that to be a solution. It was to show that your
regular expression works just fine.

location is an Object, not a string, and it has no replace() method.
location.href is a string.
 
M

mick white

RobG wrote:
[snip]
To see the properties and values of the location object for any page,
copy and paste the following as a single line into the address bar and
hit enter (you can save it as a bookmarklet):

javascript:x=location;t='';for(var p in x){t+=p+': '+x[p]+'\n';}alert(t);


Works in Firefox & IE but not Safari 1.0.3 (it may work in later versions).


Works in Safari 1.3.1 (OS 10.3.x)
Mick
 
L

Lasse Reichstein Nielsen

Lee said:
location is an Object, not a string, and it has no replace() method.
location.href is a string.

To make matters worse, the location object does have a "replace"
function. It takes a single string URL as argument and replaces
the current page with that URL's page, without adding the original
page to the history.
So it's not the same as String's replace.
/L
 
T

Thomas 'PointedEars' Lahn

Primitive string values do not have a replace() method either, they are not
String objects. What happens is that if a method is applied on a primitive
string value through a property accessor, a String container object with
that value is implicitly created which inherits the replace() method from
its prototype object.
To make matters worse, the location object does have a "replace"
function.

The function of an object, or, in other words, a function associated
with an object, is usually called a method; that includes ECMAScript
implementations:

,-[ES3]
|
| 4.2 Language Overview
|
| [...] a method is a function associated with an object via a property.
|
| [...]
| 4.3.3 Object
|
| [...] A function stored in a property of an object is called a method.

So the object referred to by `location', or `window.location' to be precise,
which is a Location host object, has a replace() method that is different
from String.prototype.replace() (no surprise here). You can display the
string value this object (or probably its `href' property if it has one)
holds with window.alert() and you can use it as-is in string concatenations
because it inherits a toString() method that returns that primitive string
value and is called implicitly on such occasions.

So, to resolve the replace problem without using the `href' property
explicitly:

location = location.toString().replace(...);
or
location = String(location).replace(...);

Where a RegExp object may be used for the first argument of .replace() and
backreferences may be used for the second argument aso. iff supported.
[...]
So it's not the same as String's replace.

Exactly.


PointedEars
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,774
Messages
2,569,596
Members
45,130
Latest member
MitchellTe
Top