Simple question

S

Simon

How can i divide a string using javascript?
For example, i've got a textfield named "attach1", the value is: "
C:\Documents and Settings\Simon\Mijn documenten\calendaroptie.txt
"

So in javascript i start with:
var chk=document.TE.attach1.value
var filename= ?

Where filename must get the value of the last "slash" (calendaroptie.txt)

How do I do that?

Thanks,
Simon
 
B

Bruno Desthuilliers

Simon a écrit :
How can i divide a string using javascript?

var s = "/some/path/to/a/file"
s.split('/')
=> ["", "some", "path", "to", "a", "file"]

HTH
 
S

Stevo

Simon said:
C:\Documents and Settings\Simon\Mijn documenten\calendaroptie.txt
var chk=document.TE.attach1.value
var filename= ?
Where filename must get the value of the last "slash" (calendaroptie.txt)
How do I do that?

how about this:

var filename=chk.substring(chk.lastIndexOf("\\"));
 
S

Simon

Ok thanks, but how do i recognize the last piece of the string?
I want to set a variable equal to the last "/". (This string can also be
variable).
I'm sorry, but I really don't know anything about javascript.
I would like it coded, all the way ;) ;)

Thanks..

Bruno Desthuilliers said:
Simon a écrit :
How can i divide a string using javascript?

var s = "/some/path/to/a/file"
s.split('/')
=> ["", "some", "path", "to", "a", "file"]

HTH
 
S

Simon

Thanks,

It works, except for one thing, now the variable "filename" isn't equal to
"calendaroptie.txt" but to "/calendaroptie.txt"
Could you help me once more? (Remove the slash)
 
R

RobG

Ok thanks, but how do i recognize the last piece of the string?

Please don't top-post, reply below trimmed quotes.

The suggested solution was:

var s = "/some/path/to/a/file"
var t = s.split('/');

so t is now an array of the bits of s split by '/'. To get the last
bit:

alert( t[t.length - 1] );

or, using a regular expression:

alert( s.replace(/^.*\//,'') );
I want to set a variable equal to the last "/". (This string can also be
variable).
I'm sorry, but I really don't know anything about javascript.
I would like it coded, all the way ;) ;)

Which makes me wonder what you are going to do with it. :)
 
S

Simon

"The suggested solution was:

var s = "/some/path/to/a/file"
var t = s.split('/');

so t is now an array of the bits of s split by '/'. To get the last
bit:

alert( t[t.length - 1] );
"

I'm sorry, doesn't seem to work.


RobG said:
Ok thanks, but how do i recognize the last piece of the string?

Please don't top-post, reply below trimmed quotes.

The suggested solution was:

var s = "/some/path/to/a/file"
var t = s.split('/');

so t is now an array of the bits of s split by '/'. To get the last
bit:

alert( t[t.length - 1] );

or, using a regular expression:

alert( s.replace(/^.*\//,'') );
I want to set a variable equal to the last "/". (This string can also be
variable).
I'm sorry, but I really don't know anything about javascript.
I would like it coded, all the way ;) ;)

Which makes me wonder what you are going to do with it. :)
 
T

Thomas 'PointedEars' Lahn

RobG said:
or, using a regular expression:

alert( s.replace(/^.*\//,'') );

window.alert(s.match(/[^\/]*$/)[0]);

is more efficient.


PointedEars
 
K

korisu

Thomas said:
RobG said:
or, using a regular expression:
alert( s.replace(/^.*\//,'') );
window.alert(s.match(/[^\/]*$/)[0]);
is more efficient.

/[^\/]+$/.exec(s);

is even more so, since you don't have to test whether or not s is a
string (or that it is even defined, if you wrap it in a function).
 

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

No members online now.

Forum statistics

Threads
474,156
Messages
2,570,878
Members
47,408
Latest member
AlenaRay88

Latest Threads

Top