473,402 Members | 2,064 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,402 software developers and data experts.

String-functions?

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
Jul 23 '05 #1
14 1284
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
Jul 23 '05 #2
"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.'
Jul 23 '05 #3


--
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
Jul 23 '05 #4
Lasse Reichstein Nielsen wrote on 18 mei 2005 in comp.lang.javascript:
"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]; }


return '';
}


you would want a string returned if there is no match.

=================

or:

function extractDomain(url) {
return /^http:\/\/([^\/]*)\/.*$/i.replace(url,'$1')
}

--
Evertjan.
The Netherlands.
(Replace all crosses with dots in my emailaddress)

Jul 23 '05 #5
Ron Eggler napisał(a):
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!


You absolutely dont need (and shouldnt use) regular expressions.
$d = $string.split('/')[2]

will do the trick.

--
zaj gezunt!
tomasz cenian tcenian at wa dot home dot pl
:::: :: : : http://cenian.boo.pl : : :: ::::
Jul 23 '05 #6
Quoth Tomasz Cenian:
Ron Eggler napisał(a):
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!


You absolutely dont need (and shouldnt use) regular expressions.

^^^^^^^^^^^^^^^^

I agree on the "don't need", but what's with the "shouldn't use"?

--
\\kristian
Jul 23 '05 #7
Kristian Thy napisaƂ(a):

You absolutely dont need (and shouldnt use) regular expressions.


^^^^^^^^^^^^^^^^

I agree on the "don't need", but what's with the "shouldn't use"?


regexps are huge and very powerful tools. I believe that if the use of
them in a particular case is not necessary (like this one) they should
not be used. This is a general programming rule. You should always use
the most efficient and straightforward method to achieve the desired thing.

--
zaj gezunt!
tomasz cenian tcenian at wa dot home dot pl
:::: :: : : http://cenian.boo.pl : : :: ::::
Jul 23 '05 #8
Quoth Tomasz Cenian:
regexps are huge and very powerful tools. I believe that if the use of
them in a particular case is not necessary (like this one) they should
not be used. This is a general programming rule. You should always use
the most efficient and straightforward method to achieve the desired thing.


Ah, okay :)

I thought you meant you shouldn't use regexps generally.

--
\\kristian
Jul 23 '05 #9
JRS: In article <3f************@individual.net>, dated Wed, 18 May 2005
22:02:08, seen in news:comp.lang.javascript, Ron Eggler
<NO****@hotmail.com> posted :
if I got a String like: http://server/dir1/dir2/dir3/
how would i extract everything between '//' and the most left single '/'?


If you are sure that there are no slashes before the //, then

S = S.split('/')[2]

will do it, except if there are browsers that won't return an empty
string for S[1] - in which case consider

S = S.split(/\/+/)[1]

which assumes that the field you want is not empty.

--
© John Stockton, Surrey, UK. ?@merlyn.demon.co.uk Turnpike v4.00 IE 4 ©
<URL:http://www.jibbering.com/faq/> JL/RC: FAQ of news:comp.lang.javascript
<URL:http://www.merlyn.demon.co.uk/js-index.htm> jscr maths, dates, sources.
<URL:http://www.merlyn.demon.co.uk/> TP/BP/Delphi/jscr/&c, FAQ items, links.
Jul 23 '05 #10
Tomasz Cenian <aa*@bb.cc.dd> writes:
You absolutely dont need (and shouldnt use) regular expressions.

$d = $string.split('/')[2]

will do the trick.


Only if the string has the correct format. There is no check for this.

It will still do too much work, since creating the other substrings is
not necessary.

var i1 = string.indexOf("//") + 2;
var i2 = string.indexOf("/", i1);
return string.substring(i1,i2);

/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.'
Jul 23 '05 #11
Lasse Reichstein Nielsen wrote:
"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]; }
}


Cool, but allowing for Mozilla-style - file:///usr/...
and IE-style - file://c:\...
and putting in the colon ':' for added safety, how about:

var match = /:\/{2,3}([^\/|\\]*)(\/|\\)/.exec(url);

--
Rob
Jul 23 '05 #12
RobG <rg***@iinet.net.auau> writes:
Cool, but allowing for Mozilla-style - file:///usr/...


That's not just Mozilla-style. It's the official shorthand for
file://localhost/usr/...

/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.'
Jul 23 '05 #13
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!


You're looking for "server"?

Str=Str.split("//")[1].split("/")[0];

But if this a location object, there are several built-in properties.
Mick
Jul 23 '05 #14
Tomasz Cenian wrote:
Kristian Thy napisa?(a):
You absolutely dont need (and shouldnt use) regular expressions.

^^^^^^^^^^^^^^^^
I agree on the "don't need", but what's with the "shouldn't use"?


regexps are huge and very powerful tools. I believe that if the use of
them in a particular case is not necessary (like this one) they should
not be used. This is a general programming rule. You should always use
the most efficient and straightforward method to achieve the desired
thing.


You seldom know the length of the string to be parsed and so regular
expressions are in most cases the most efficient and straightforward
method.
PointedEars
Jul 23 '05 #15

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

Similar topics

16
by: Krakatioison | last post by:
My sites navigation is like this: http://www.newsbackup.com/index.php?n=000000000040900000 , depending on the variable "n" (which is always a number), it will take me anywhere on the site......
5
by: Stu Cazzo | last post by:
I have the following: String myStringArray; String myString = "98 99 100"; I want to split up myString and put it into myStringArray. If I use this: myStringArray = myString.split(" "); it...
9
by: John F Dutcher | last post by:
I use code like the following to retrieve fields from a form: recd = recd.append(string.ljust(form.getfirst("lname",' '),15)) recd.append(string.ljust(form.getfirst("fname",' '),15)) etc.,...
9
by: Derek Hart | last post by:
I wish to execute code from a string. The string will have a function name, which will return a string: Dim a as string a = "MyFunctionName(param1, param2)" I have seen a ton of people...
10
by: Angus Leeming | last post by:
Hello, Could someone explain to me why the Standard conveners chose to typedef std::string rather than derive it from std::basic_string<char, ...>? The result of course is that it is...
2
by: Andrew | last post by:
I have written two classes : a String Class based on the book " C++ in 21 days " and a GenericIpClass listed below : file GenericStringClass.h // Generic String class
2
by: Badass Scotsman | last post by:
Hello, Using VB and ASP,NET I would like to be able to search a STRING for a smaller STRING within, based on the characters which appear before and after. For example: String1 = " That was...
15
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
11
by: ramu | last post by:
Hi, Suppose I have a string like this: "I have a string \"and a inner string\\\" I want to remove space in this string but not in the inner string" In the above string I have to remove...
8
by: drjay1627 | last post by:
hello, This is my 1st post here! *welcome drjay* Thanks! I look answering questions and getting answers to other! Now that we got that out of the way. I'm trying to read in a string and...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
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,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
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,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

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.