[Javascript] parse question

Emmanuel emmanuel at quomodo.com
Mon Feb 26 01:01:07 CST 2007


At 9:03 PM +0000 2/25/07, Jonathan Buchanan wrote:
>tedd wrote:
>>Hi gang:
>>
>>I've reviewed several methods to parse strings, but what's the 
>>shortest/easiest method to parse the sting from:
>>
>>http://www.mydomain.com/firstdir/secondir/myfile.txt
>>
>>to:
>>
>>myfile.txt   ?
>
>One possible method:
>
>var s = "http://www.mydomain.com/firstdir/secondir/myfile.txt";
>s.split("/").pop();

That's very fine, and my solution is not better, but I love regular 
expressions:

var s = "http://www.mydomain.com/firstdir/secondir/myfile.txt";
s = s.replace ( /.*\// , "" )

means: change (to the empty string) the longest string (. = whatever, 
* = whatever number of times) ending with slash (escaped with 
backslash, since slash is the termination character for the regular 
expression).

Emmanuel



More information about the Javascript mailing list