如何在 JavaScript 中将 URL 按正斜杠进行分割?


要拆分一个 URL,使用 split() 方法。在此之前,使用 toString()。让我们看一个例子

示例

var newURL="http://www.example.com/index.html/homePage/aboutus/";
console.log(newURL);
var splitURL=newURL.toString().split("/");
console.log(splitURL);

上面,我们在 split() 函数中设置了正斜杠,因为我们需要在每个这样的斜杠后面拆分 URL。

要运行上述程序,您需要使用以下命令 −

node fileName.js.

输出

这里,我的文件名是 demo150.js。这将产生以下输出 −

PS C:\Users\Amit\JavaScript-code> node demo150.js
http://www.example.com/index.html/homePage/aboutus/[
   'http:',
   '',
   'www.example.com',
   'index.html',
   'homePage',
   'aboutus',
   ''
]

更新日期:11-9-2020

8K+ 浏览

开始你的 职业生涯

通过完成课程来进行认证

开始
广告
© . All rights reserved.