akdev
asked on
How do I match URL parts
I am trying to add "www" to the front of my URL if the user didn't add it. The problem is that I have several possible entries that I don't want changed.
The addresses I don't want changed are as follows:
www.sitename.com
origin.sitename.com
ads.sitename.com
172.sitename.com
in.sitename.com
192.sitename.com
But, if the URL is "http://sitename.com" or "sitename.com", I need it to change to "http://www.sitename.com" or "www.sitename.com" respectively.
Here is my code:
<script type="text/javascript">
var oldUrl = window.location.href;.
var myRegExp = "www.|origin.|ads.|172.|in .|192.";
var string1 = window.location.href;
var matchPos1 = string1.search(myRegExp);
if(matchPos1 != -1)
// this is where I need to rewrite the URL
</script>
Another problem is that the "in" part is in some of the other parts of the URL.
Thanks in advance for the help.
The addresses I don't want changed are as follows:
www.sitename.com
origin.sitename.com
ads.sitename.com
172.sitename.com
in.sitename.com
192.sitename.com
But, if the URL is "http://sitename.com" or "sitename.com", I need it to change to "http://www.sitename.com" or "www.sitename.com" respectively.
Here is my code:
<script type="text/javascript">
var oldUrl = window.location.href;.
var myRegExp = "www.|origin.|ads.|172.|in
var string1 = window.location.href;
var matchPos1 = string1.search(myRegExp);
if(matchPos1 != -1)
// this is where I need to rewrite the URL
</script>
Another problem is that the "in" part is in some of the other parts of the URL.
Thanks in advance for the help.
ASKER
Thanks jmatix,
This is really close, but the following is happening:
This is the address I am going to
http://sitename.com/item_detail.html
And the code is trying to put the "www." in front of the "http".
This is the error message I am getting.
Firefox doesn't know how to open this address, because the protocol (www.http) isn't associated with any program.
Any ideas?
Thanks.
This is really close, but the following is happening:
This is the address I am going to
http://sitename.com/item_detail.html
And the code is trying to put the "www." in front of the "http".
This is the error message I am getting.
Firefox doesn't know how to open this address, because the protocol (www.http) isn't associated with any program.
Any ideas?
Thanks.
Try this:
var oldUrl = window.location.href;
if (!oldUrl.match(/^(https?:\ /\/)?(www\ .|origin\. |ads\.|172 \.|in\.|19 2\.)/))
window.location.href = oldUrl.replace(/^(https?:\ /\/)?/i, "$1www.");
var oldUrl = window.location.href;
if (!oldUrl.match(/^(https?:\
window.location.href = oldUrl.replace(/^(https?:\
ASKER
imatix,
I put in the code. Can't get it to work. To test it, I am at address, http://172.16.1.12/item_detail.html. I changed the "172." in the match string to "173." to see if it changed my URL for me. It didn't.
Thanks.
I put in the code. Can't get it to work. To test it, I am at address, http://172.16.1.12/item_detail.html. I changed the "172." in the match string to "173." to see if it changed my URL for me. It didn't.
Thanks.
Try this:
var oldUrl = window.location.href;
window.location.href = oldUrl.replace(/^(https*:\/\/)*((?!www|ads|origin|172|192|in).+)*$/img, "$1www.$2");
ASKER
In IE8, I am getting an error that says it is expecting a ")" in reg ex. It looks like all the parenthesis are there.
The line it is complaining about is:
if(!oldUrl.match(/^(https? :\/\/)?(ww w\.|origin \.|ads\.|1 73123\.|in \.|192\.)/ ))
The line it is complaining about is:
if(!oldUrl.match(/^(https?
ASKER
IE8 is also erroring with the message for the new line you gave:
window.location.href = oldUrl.replace(/^(https*:\ /\/)*((?!w ww|ads|ori gin|172|19 2|in).+)*$ /img, "$1www.$2");
With this line, I am thinking I don't need the "if" statement, correct?
window.location.href = oldUrl.replace(/^(https*:\
With this line, I am thinking I don't need the "if" statement, correct?
When I try http://173.16.1.12/item_detail.html, I get:
http://www.173.16.1.12/item_detail.html
That is the expected output. Right?
http://www.173.16.1.12/item_detail.html
That is the expected output. Right?
With this line, I am thinking I don't need the "if" statement, correct?Yes correct! Just two lines i gave. Work fine in IE8.
Try this code, is he work correct?
<script type="text/javascript">
var mass = [ "www.sitename.com",
"origin.experts-exchange.com",
"ads.sdobe.com",
"172.sitename.com",
"in.sitename.com",
"192.sitename.com",
"http://sitename.com/",
"https://sitename.com/",
"sitename.com",
"zzz.microsoft.com",
"10.sitename.com"
];
var tempstr;
for(var i=0; i<mass.length; i++) {
tempstr = mass[i].replace(/^(https*:\/\/)*((?!www|ads|origin|172|192|in).+)*$/img, "$1www.$2");
alert(mass[i]+"---"+tempstr);
}
</script>
ASKER
jmatix,
This is the code that I am using from your suggestion. It is giving me a js error on line 121, char 28 in IE8.
line 119: <script type="text/javascript">
line 120: var oldUrl = window.location.href;
line 121: if(!oldUrl.match(/^(https? ://)?(www. |origin.|a ds.|173123 .|in.|192. )/))
line 122: window.location.href = oldUrl.replace(/^(https*:/ /)*((?!www |ads|origi n|173|192| in).+)*$/i mg, "$1www.$2");
line 123: </script>
-------------------------- ---------- ---------- ---------- ---------- ---------- ----------
kivan24,
This is the code that I am using from your suggestion. It is giving me a js error on line 123, char 48 in IE8.
line 121: <script type="text/javascript">
line 122: var oldUrl = window.location.href;
line 123: window.location.href = oldUrl.replace(/^(http*:// )*((?!www| ads|origin |173|192|i n).+)*$/im g, "$1www.$2");
line 124: </script>
This is the code that I am using from your suggestion. It is giving me a js error on line 121, char 28 in IE8.
line 119: <script type="text/javascript">
line 120: var oldUrl = window.location.href;
line 121: if(!oldUrl.match(/^(https?
line 122: window.location.href = oldUrl.replace(/^(https*:/
line 123: </script>
--------------------------
kivan24,
This is the code that I am using from your suggestion. It is giving me a js error on line 123, char 48 in IE8.
line 121: <script type="text/javascript">
line 122: var oldUrl = window.location.href;
line 123: window.location.href = oldUrl.replace(/^(http*://
line 124: </script>
You need to escape / and . with \ as I had given:
var oldUrl = window.location.href;
if (!oldUrl.match(/^(https?:\ /\/)?(www\ .|origin\. |ads\.|172 \.|in\.|19 2\.)/))
window.location.href = oldUrl.replace(/^(https?:\ /\/)?/i, "$1www.");
var oldUrl = window.location.href;
if (!oldUrl.match(/^(https?:\
window.location.href = oldUrl.replace(/^(https?:\
ASKER
Sorry imatix,
That was the view source I listed before. This is what is in the actual code:
<script type="text/javascript">
var oldUrl = window.location.href;
if (!oldUrl.match(/^(https?:\ /\/)?(www\ .|origin\. |ads\.|172 \.|in\.|19 2\.)/))
window.location.href = oldUrl.replace(/^(https?:\ /\/)?/i, "$1www.");
</script>
There is still a js error saying "Expected ')' in regular expression".
Thanks.
That was the view source I listed before. This is what is in the actual code:
<script type="text/javascript">
var oldUrl = window.location.href;
if (!oldUrl.match(/^(https?:\
window.location.href = oldUrl.replace(/^(https?:\
</script>
There is still a js error saying "Expected ')' in regular expression".
Thanks.
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
Awesome imatix,
That worked. Thanks so much for your help and diligence.
That worked. Thanks so much for your help and diligence.
ASKER
Stayed with my problem until solved. Nice job.
var oldUrl = window.location.href;
if (!oldUrl.match(/^(www\.|or
window.location.href = "www."+oldUrl;