1

Here is my issue hope that someone can help me:

//Declartions

var DBName = somthing; // changed dynamically 
var lastPiece;

I have this string:

"../PeopleImages/GGP/Person_3281.jpg"

I want to grab "Person_3281.jpg" piece in the string save it into a variable lastPiece = Person_3281.jpg; (The last piece is been change dynamically).

and after that combined it to another string for example:

var address = "https://www.google.com/gs/companyCode="+ DBName + lastPiece ;

how can I do it in Javascript??

Huy Zukerman
  • 215
  • 1
  • 3
  • 11

2 Answers2

1
var string = "../PeopleImages/GGP/Person_3281.jpg";
var arrFromString = string.split('/');

var lastPiece = arrFromString[arrFromString.length -1];
martskins
  • 2,920
  • 4
  • 26
  • 52
0

You can achieve this with a pretty simple regex:

var originalstring = "../PeopleImages/GGP/Person_3281.jpg";
var lastpiece = /[^/]*$/.exec(originalstring)[0];

reference

Community
  • 1
  • 1
JRulle
  • 7,448
  • 6
  • 39
  • 61