[英]how do I parse github url with javascript?
I need to parse a GitHub URL like https://github.com/<USERNAME>/<REPO>
, but however my frontend is done in reactjs, so I am trying to figure out the following code in python for javascript url.rsplit('/',1)[1].split('.')[0]
这将得到<REPO>
作为结果。 不是 python 等效代码,但可以工作
const url = 'https://github.com/<USERNAME>/<REPO>'; const repoName = url.split('/').pop(); console.log(repoName);
浏览器内置的URL
class 可以为您完成大部分基本的 url 解析,并有助于确保 repo 字符串不包含类似的任何参数。
const url = new window.URL("https://github.com/username/repo.git"); // Split the path by '/' and remove any empty array items // Empty items might be caused if the user enters '//' as part of the url const pathArr = url.pathname.split("/").filter(Boolean); const repo = pathArr[1]; console.log(repo)
有关URL
class 的更多信息,请访问: https://developer.mozilla.org/en-US/docs/Web/API/URL
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.