在Javascript中重新定义字符串

时间:2015-01-27 18:02:27

标签: javascript

这是我的问题,希望有人可以帮助我:

// Declartions

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

我有这个字符串:

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

我想在字符串中抓取“Person_3281.jpg”片段将其保存到变量中 lastPiece = Person_3281.jpg; (最后一块是动态变化的。)

然后将它组合成另一个字符串,例如:

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

我怎么能在Javascript中做到?

2 个答案:

答案 0 :(得分:1)

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

var lastPiece = arrFromString[arrFromString.length -1];

答案 1 :(得分:0)

你可以用一个非常简单的正则表达式实现这个目的:

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

reference