对于这个网址
"http://testsite/sites/TestSubSite/objstrat/CultureConnection/Pages/default.aspx"
尝试提取企业/站点之后的所有内容。
我想要 "/TestSubSite/objstrat/CultureConnection/Pages/default.aspx"
我可以这样获取文件名:
var filename = parenturl.substring(parenturl.lastIndexOf("/") + );
但不知道如何从中间提取..
最佳答案
对于片段或查询中带有 /
的任何 URI 引用,使用 lastIndexOf
将会失败,如 http://example.com/foo/bar# 中所示/baz/boo
.
类似 Google Closure's Uri module 的库可以让这变得容易。
new goog.Uri(parenturl).getPath()
<小时/>
如果您选择的库没有 URI 支持,则 http://www.ietf.org/rfc/rfc3986.txt附录 B 建议
var urlRegex = /^(([^:\/?#]+):)?(\/\/([^\/?#]*))?([^?#]*)(\?([^#]*))?(#(.*))?/;
var path = decodeURIComponent(urlRegex.exec(parenturl)[5]);
将为您提供路径部分。
<小时/>一旦获得路径,您就可以通过执行类似的操作来删除 /sites/
部分
var pathSuffix = path.replace(/^\/sites\//, '/');
关于javascript - 从父 URL 中提取部分 URL,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14756295/