如何在 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', '' ]
广告
数据结构
网络
关系数据库管理系统
操作系统
Java
iOS
HTML
CSS
Android
Python
C 编程
C++
C#
MongoDB
MySQL
Javascript
PHP