我有一个像url的字符串 " example.com/Apps/Visitor/visitscanner" , " example.com/Apps/Seatings/visitscanner
我想在"" http://example.com/Apps/"之后选择目录名称。
以上示例网址,如果我的网址字符串为" http://example.com/Apps/Visitor/visitscanner"这一个,结果应该像"访客"。
我如何在javascript中执行此操作?
答案 0 :(得分:2)
您可以使用split(),例如:
var str = "example.com/Apps/Visitor/visitscanner";
var parts = str.split('/');
console.log( parts[parts.length - 2] ); //Visitor
答案 1 :(得分:0)
如果您想匹配"http://example.com/Apps/Visitor/visitscanner/foo"
或"http://example.com/Apps/Visitor"
等网址,可以使用正则表达式执行此操作...
var url = "http://example.com/Apps/Visitor/visitscanner";
var match = /^(https?:\/\/)?example.com\/apps\/([^/]+)/i.exec(url);
var directory = match[2]; // get the second match group ("Visitor")
答案 2 :(得分:0)
你可以使用正则表达式: -
var url = "http://example.com/Apps/Visitor/visitscanner";
var dirName = /\/\/.+?\/Apps\/(.+?)(?=\/)/g.exec(url)[1];