在 JavaScript 中,在每个斜杠后拆分 URL?

javascriptweb developmentobject oriented programming更新于 2025/11/16 6:04:17

要拆分 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',
   ''
]

相关文章


有用资源