字符串的功能呢? [英] String-functions?

查看:67
本文介绍了字符串的功能呢?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述




如果我得到的字符串如下: http:// server / dir1 / dir2 / dir3 /

如何在''//''和最左边的''/''之间提取所有内容?

谢谢!

-

roN
www.rideon.ch

解决方案

Ron Eggler写道:



如果我得到一个字符串,如: http:// server / dir1 / dir2 / dir3 /
如何在''//'和最左边的单''/'之间提取所有内容?
谢谢你!




var s =''http:// server / dir1 / dir2 / dir3 /'';

s = s.match (/ \ / \ /([^ \ /] *)\ //);

s = s [1];

alert(s);


-

Justin Koi visto - ju****@koivi.com
http://koivi.com


" Ron Eggler" < NO **** @ hotmail.com>写道:

如果我得到一个字符串: http:// server / dir1 / dir2 / dir3 /
如何在''//''和最左边的''/''之间提取所有内容?



听起来像是正则表达式的工作:


函数extractDomain(url){

var match = / \ / \ /([^ \ /] *)\ // EXEC(URL)。 // regexp //([^ /])* /

if(match){return match [1]; }

}


/ L

-

Lasse Reichstein Nielsen - lr*@hotpop.com

DHTML死亡颜色:< URL:http://www.infimum.dk/HTML/rasterTriangleDOM .html>

''没有判断的信仰只会降低精神神圣。''





-

Grant Wagner< gw ***** @ agricoreunited.com>

comp.lang.javascript常见问题 - http://jibbering.com/faq

" Ron Eggler" < NO **** @ hotmail.com>在消息中写道

新闻:3f ************ @ individual.net ...


如果我得到如下字符串: http:// server / dir1 / dir2 / dir3 /
如何在''//'和最左边的单个
''/''之间提取所有内容?




< script type =" text / javascript">

var s =''http:// server / dir1 / dir2 / dir3 /'';

var start = s.indexOf(''//'');

var end = s.lastIndexOf(''/'');

alert(s.substring(开始+ 2,结束));

< / script>


如果

string(或只有两个 - http://)

您还可以使用正则表达式来提取您需要的信息。


如果网址实际上是我在location对象中,您可以使用location.host和location.pathname< url:

信息.sun.com / source / 816-6408-10 / location.htmtarget =_ blank> http://docs.sun.com/source/816-6408-10/location.htm
/> ;。


-

Grant Wagner< gw ***** @ agricoreunited.com>

comp.lang .javascript常见问题 - http://jibbering.com/faq


Hi,

if I got a String like: http://server/dir1/dir2/dir3/
how would i extract everything between ''//'' and the most left single ''/''?
Thank you!
--
roN
www.rideon.ch

解决方案

Ron Eggler wrote:

Hi,

if I got a String like: http://server/dir1/dir2/dir3/
how would i extract everything between ''//'' and the most left single ''/''?
Thank you!



var s = ''http://server/dir1/dir2/dir3/'';
s = s.match(/\/\/([^\/]*)\//);
s = s[1];
alert(s);

--
Justin Koivisto - ju****@koivi.com
http://koivi.com


"Ron Eggler" <NO****@hotmail.com> writes:

if I got a String like: http://server/dir1/dir2/dir3/
how would i extract everything between ''//'' and the most left single ''/''?



Sounds like a job for regular expressions:

function extractDomain(url) {
var match = /\/\/([^\/]*)\//.exec(url); // regexp //([^/])*/
if (match) { return match[1]; }
}

/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.''




--
Grant Wagner <gw*****@agricoreunited.com>
comp.lang.javascript FAQ - http://jibbering.com/faq
"Ron Eggler" <NO****@hotmail.com> wrote in message
news:3f************@individual.net...

Hi,

if I got a String like: http://server/dir1/dir2/dir3/
how would i extract everything between ''//'' and the most left single
''/''?



<script type="text/javascript">
var s = ''http://server/dir1/dir2/dir3/'';
var start = s.indexOf(''//'');
var end = s.lastIndexOf(''/'');
alert(s.substring(start + 2, end));
</script>

This will fail in most interesting ways if there are no slashes in the
string (or only two - http://).

You can also use regular expressions to extract the information you
seek.

And if the url is actually in the location object, then you can extract
the information using location.host and location.pathname <url:
http://docs.sun.com/source/816-6408-10/location.htm />.

--
Grant Wagner <gw*****@agricoreunited.com>
comp.lang.javascript FAQ - http://jibbering.com/faq


这篇关于字符串的功能呢?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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