473,803 Members | 3,833 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

issues getting and regex'ing a url

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(curr ent_url)) {

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

window.alert("f ound :: " + me + " :: " + current_url);
} else {
window.alert("n ot");
}

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
14 2927
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.lo cation.protocol );
// gives you "file:" or "http:" or
// wherever you are.

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

Jan 9 '06 #2
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(wind ow.content.loca tion)) {

var me = window.content. location.replac e(re,"http://www.addme.com/");
window.alert("f ound :: " + me);
} else {
window.alert("n ot");
}

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
daveyand wrote on 09 jan 2006 in comp.lang.javas cript:
if(re.test(wind ow.content.loca tion)) {

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


1 "location" is a short for "location.h ref"

2 what is "content"? use "document"

try:

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

--
Evertjan.
The Netherlands.
(Please change the x'es to dots in my emailaddress)
Jan 9 '06 #4
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
daveyand wrote on 09 jan 2006 in comp.lang.javas cript:
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
VK

daveyand wrote:
hey VK,

Not so sure what you're telling me here


I'm telling you that window.location / document.locati on 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.co m/mypage.html" you want to have
"www.myserver.c om/mypage.html" then in HTTP it's called *pathname* and
the relevant string is
document.locati on.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
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
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.locati on.pathname;

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

Jan 9 '06 #9
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=lo cation;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

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

0
1709
by: Jonas Galvez | last post by:
Hi, I'm trying to parse some binary data with regexes. It works well in the latest Python build, but I need to run this on Python 1.5.2. The binary data has a pattern like this: keyName1\002..(.*)\000.*keyName2\002..(.*)\000 (I'm using regex syntax to illustrate) So I wrote the following script:
7
2578
by: Dan Stromberg | last post by:
I'm working on writing a program that will synchronize one database with another. For the source database, we can just use the python sybase API; that's nice and normal. For the target database, unfortunately, the only interface we have access to, is http+html based. There's same javascript involved too, but hopefully we won't have to interact with that. So, I've got Basic AUTH going with http, but now I'm faced with the following...
3
265
by: DevBoy | last post by:
I am in need of parsing string based on characters like / or { or } or ^ However, anytime I try and run the following code I do not the proper results (It always returns the same string unparsed. Regex rx = new Regex("{"); string segmentData = rx.split(str); Any helpful hints?
2
1185
by: Dany C. | last post by:
We have a client application running on VS2005 RC. Here is the issues. Each time the application is restrart, the fisrt webservice call takes a long time to complete (sometime more that 2 minutes). All subsequent call to the same webservice are done in few second. Is there something that can be done to speed up the first web service call? Regards, --
17
1997
by: garrickp | last post by:
While creating a log parser for fairly large logs, we have run into an issue where the time to process was relatively unacceptable (upwards of 5 minutes for 1-2 million lines of logs). In contrast, using the Linux tool grep would complete the same search in a matter of seconds. The search we used was a regex of 6 elements "or"ed together, with an exclusionary set of ~3 elements. Due to the size of the files, we decided to run these line...
3
2290
by: Masa Ito | last post by:
I am trying to capture the contents of a function with Regex. I am using Expresso to test (nice - thanks for the great tool UltraPico!). I can handle my own with single line regex's (I think).. I want to have a named capture of the entire 'contents' of specific functions. EG: Sample code <Description("{0} is a required field.")_ Protected Overridable Function AccountIDRequired(ByVal target As Object, ByVal e As RuleArgs) As Boolean...
15
50272
by: morleyc | last post by:
Hi, i would like to remove a number of characters from my string (\t \r \n which are throughout the string), i know regex can do this but i have no idea how. Any pointers much appreciated. Chris
16
2256
by: Mark Chambers | last post by:
Hi there, I'm seeking opinions on the use of regular expression searching. Is there general consensus on whether it's now a best practice to rely on this rather than rolling your own (string) pattern search functions. Where performance is an issue you can alway write your own specialized routine of course. However, for the occasional pattern search where performance isn't an issue, would most seasoned .NET developers rely on "Regex" and...
7
2066
by: Nightcrawler | last post by:
Hi all, I am trying to use regular expressions to parse out mp3 titles into three different groups (artist, title and remix). I currently have three ways to name a mp3 file: Artist - Title Artist - Title (Remix) Artist - Title
0
9699
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
10309
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
10289
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
9119
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
6840
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5625
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
4274
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
3795
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2968
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.