js截取指定字符前面或後面的內容

function getCaption(obj,state) {
var index=obj.lastIndexOf("\-");
if(state==0){
obj=obj.substring(0,index);
}else {
obj=obj.substring(index+1,obj.length);
}
return obj;
}
var data = 'aaa-bbb'
//截取符號前面部分
getCaption(data,0) //輸出aaa
//截取符號後面部分
getCaption(data,1) //輸出bbb

原文連接:https://blog.csdn.net/caiyongshengCSDN/article/details/88420416spa

相關文章
相關標籤/搜索