By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
434,871 Members | 2,575 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 434,871 IT Pros & Developers. It's quick & easy.

issues getting and regex'ing a url

P: n/a
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/...ptexample.html .

Cheers for any help guys.

Jan 9 '06 #1
Share this Question
Share on Google+
14 Replies


P: n/a
VK

daveyand wrote:
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

Jan 9 '06 #2

P: n/a
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?

Jan 9 '06 #3

P: n/a
daveyand wrote on 09 jan 2006 in comp.lang.javascript:
if(re.test(window.content.location)) {

var me =
window.content.location.replace(re,"http://www.addme.com/");


1 "location" is a short for "location.href"

2 what is "content"? use "document"

try:

window.document.location.href.replace(re,"http://www.addme.com/");

--
Evertjan.
The Netherlands.
(Please change the x'es to dots in my emailaddress)
Jan 9 '06 #4

P: n/a
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?

Jan 9 '06 #5

P: n/a
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.

--
Evertjan.
The Netherlands.
(Please change the x'es to dots in my emailaddress)
Jan 9 '06 #6

P: n/a
VK

daveyand wrote:
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

Jan 9 '06 #7

P: n/a
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;

???

Jan 9 '06 #8

P: n/a
VK

daveyand wrote:
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?

Jan 9 '06 #9

P: n/a
daveyand wrote:
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).

--
Rob
Jan 9 '06 #10

P: n/a
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 :)

Jan 9 '06 #11

P: n/a
daveyand wrote on 09 jan 2006 in comp.lang.javascript:
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. :)


with (window.content.location) {
url = "http://www.addme.com/" + hostname + pathname;
}

--
Evertjan.
The Netherlands.
(Please change the x'es to dots in my emailaddress)
Jan 9 '06 #12

P: n/a
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
Jan 9 '06 #13

P: n/a
Lee <RE**************@cox.net> writes:
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
--
Lasse Reichstein Nielsen - lr*@hotpop.com
DHTML Death Colors: <URL:http://www.infimum.dk/HTML/rasterTriangleDOM.html>
'Faith without judgement merely degrades the spirit divine.'
Jan 10 '06 #14

P: n/a
Lasse Reichstein Nielsen wrote:
Lee <RE**************@cox.net> writes:
location is an Object, not a string, and it has no replace() method.
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.
location.href is a string.
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
Jan 10 '06 #15

This discussion thread is closed

Replies have been disabled for this discussion.