如何在 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