Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Get string piece after last symbols "/" or "\"

I need get from string, piece after last \ or /, that is from this string C:\fake\path\some.jpg result must be some.jpg

I tried this:

var str = "C:\fake\path\some.jpg";
var newstr = str.replace(/[^\\\/]+$/, "");
alert(newstr);

http://jsfiddle.net/J4GdN/3/

but not works, what is right regex for this?

like image 947
Oto Shavadze Avatar asked Dec 02 '22 23:12

Oto Shavadze


1 Answers

You don't need a regex to do it, this should work:

var newstr = str.substring(Math.max(str.lastIndexOf("\\"), str.lastIndexOf("/")) + 1);
like image 174
Jalayn Avatar answered Dec 16 '22 14:12

Jalayn