问题得到和正则表达的网址 [英] issues getting and regex'ing a url

查看:61
本文介绍了问题得到和正则表达的网址的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

嘿伙计们,


我已经花了很多时间搜索网络,而且这个团队也不能...... / b $ b似乎找到了我的解决方案问题。


基本上我想获取当前网址,然后将

替换为http://其他内容。


这是当前代码。

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);

}否则{

window.alert(不是);

}


如果我的页面是 http://ww.google.com '会收到警告:


found :: undefined :: http://www.google。 COM


我不明白为什么我会变得不确定。当re.test()工作时。

肯定这意味着正则表达式是正确的。我使用了相同的代码:
http:// www.regular-expressions.info/...ptexample.html


为任何帮助人员干杯。

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.

推荐答案



daveyand写道:

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.




如果没有人阅读它们,手册的价值是多少? :-))

alert(window.location.protocol);

//给你file:或http:或者

//无论你在哪里。


window.location.protocol =" https:" ;;

//通过SSL查看当前页面



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


嘿VK,


不太确定你在告诉我什么在这里,似乎没有帮助我的问题

。但这是非常有用的东西,并且可以在将来的某个地方帮助我b $ b。


Lee,


g的点,只是测试,我无法让它工作所以我添加了g

也尝试过我。它不需要我知道:)


至于你的解决方案,它是一个奇怪的,我的意思是它的工作原理。但显然

我希望这是动态的,(也许我应该说明了???)。我已经尝试了这个,但是再次无法工作:


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");

}


再次我们得到同样的东西,我只是没有定义。虽然权利应该是

。看到我们进入逻辑区。


但是我可以看到你让它运转起来,我只是想看看它是什么。你需要在perl中点击或修剪JS吗?

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?


daveyand于2006年1月9日写道comp.lang.javascript
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/");
if(re.test(window.content.location)) {

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




1" location" ;是location.href的缩写


2什么是内容?使用文档


尝试:


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


-

Evertjan。

荷兰。

(请在我的电子邮件地址中将x'变为点数)



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)


这篇关于问题得到和正则表达的网址的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆