js截取指定字符前面或后面的内容

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/caiyongshengCSDN/article/details/88420416
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

猜你喜欢

转载自blog.csdn.net/caiyongshengCSDN/article/details/88420416