js 获取地址第1或2个斜杠后的内容或地址前一部分

var value = "https://www.baidu.com/20210922/95b22ab5034b2d0f.jpg";
const str = value.split("https://"); //https://进行分割,
const index = str[1].indexOf("/")+1; //indexOf 获取第一个斜杠的索引,
console.log(str[1].substring(index)); //substring 截取,

 

let str = "zheng/shize/zsz";
let one; // 第二个斜杠前内容
let two; // 第二个斜杠后内容
let first = str.indexOf("/") + 1; // 从第一个斜杠算起(+1表示不包括该横杠)
let kong = str.indexOf(" ", first); // 第一个斜杠后的第一个空格
let heng = str.indexOf("/", first); // 第一个斜杠后的第一个斜杠(即第二个斜杠)

//

//let heng = str.indexOf("/", first)+1; // 第一个斜杠后的第一个斜杠(即第二个斜杠)

//let heng2 = str.indexOf("/", heng); // 第二个斜杠后的第一个斜杠(即第三个斜杠)


if (heng == -1) {
one = str.substring(0, kong);
two = str.substring(kong).substring(1, str.length);
} else {
one = str.substring(0, heng);
two = str.substring(heng).substring(1, str.length);
}
console.log(one);
console.log(two);

//获取前面的域名

var i = "https://www.baidu.com/20210922/95b22ab5034b2d0f.jpg";
console.log(i.match(/^(?:[^\/]|\/\/)*/));

 

posted on 2021-09-22 14:28  多言  阅读(3378)  评论(0编辑  收藏  举报

导航