String-functions?

L

Lasse Reichstein Nielsen

Ron Eggler said:
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
 
G

Grant Wagner

--
Grant Wagner <[email protected]>
comp.lang.javascript FAQ - http://jibbering.com/faq
Ron Eggler said:
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 />.
 
E

Evertjan.

Lasse Reichstein Nielsen wrote on 18 mei 2005 in comp.lang.javascript:
Ron Eggler said:
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')
}
 
K

Kristian Thy

Quoth Tomasz Cenian:
Ron Eggler 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"?
 
T

Tomasz Cenian

Kristian Thy napisał(a):
^^^^^^^^^^^^^^^^

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.
 
K

Kristian Thy

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.
 
D

Dr John Stockton

JRS: In article <[email protected]>, dated Wed, 18 May 2005
22:02:08, seen in Ron Eggler
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.
 
L

Lasse Reichstein Nielsen

Tomasz Cenian said:
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
 
R

RobG

Lasse said:
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);
 
L

Lasse Reichstein Nielsen

RobG said:
Cool, but allowing for Mozilla-style - file:///usr/...

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

/L
 
T

Thomas 'PointedEars' Lahn

Tomasz said:
Kristian Thy napisa?(a):

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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
474,266
Messages
2,571,089
Members
48,773
Latest member
Kaybee

Latest Threads

Top