0

I have string like #ls/?folder_path=home/videos/

how i can find last text from string? this place is videos

other strings like

  • #ls/?folder_path=home/videos/
  • #ls/?folder_path=home/videos/test/testt/
  • #ls/?folder_path=seff/test/home/videos/
2
  • You should provide at least a few different strings... Commented Apr 6, 2011 at 6:13
  • Do you always have / at the end? Commented Apr 6, 2011 at 6:14

7 Answers 7

3

We could use a few more example strings, but based off of your one and only example, here's a rough regex to get you started:

.*?/\?.*?/(.*?)\//

EDIT:

Based on your extended examples:

.*?/\?.*/(.*?)\//

This regex will consume text until the second to last / and capture until the last / in the string.

Sign up to request clarification or add additional context in comments.

Comments

1

This will work even if the string doesn't end in / var str;

var re = /\w+(?=\/?$)/;

str = "#ls/?folder_path=home/videos/"
str.match(re) ; //# => videos

str = "#ls/?folder_path=home/videos/test/testt/"
str.match(re) ; //# => testt

str = "#ls/?folder_path=seff/test/home/videos/"
str.match(re) ; //# => videos


str = "#ls/?folder_path=home/videos/test/testt"
str.match(re) ; //# => testt

Comments

1
\/([^\/]*)\/?$

This regex will match all non / between the last two /. Where the last / is optional. The $ is matching the end of the string.

Your resulting string is then in the first capturing group (because of the ()) $1

You can test it here

Comments

1

There are many ways to do this. One of them:

var str = '#ls/?folder_path=home/videos/'.replace(/\/$/,'');
alert(str.substr(str.lastIndexOf('/')+1)); //=> videos

Alternative without using replace

var str = '#ls/?folder_path=home/videos/'
   ,str = str.substr(0,str.length-1)
   ,str = str.substr(str.lastIndexOf('/')+1);
alert(str); //=> videos

Comments

0

If your data is consistent like this string, this is a simple split based way to retreive your required string: http://jsfiddle.net/EEkLP/

var str="#ls/?folder_path=home/videos/";
var strArr = str.split("/");
alert(strArr[strArr.length-2]);

Comments

0

If it always ends with / then this will works.

var str = '#ls/?folder_path=home/videos/';
var arr = str.split('/');
var index = arr.length-2;
console.log(arr[index]);

Comments

0

If the last word always enclosed with forward slashes, then you can try this -

".+\/([^\/]+)\/$"

or in regex notation

/.+\/([^\/]+)\/$/

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.