JavaScript驗證一個url的方法總結
1.使用 URL 構造函數(shù)來驗證 URL
當傳遞一個字符串給 URL 構造函數(shù)時,如果字符串是一個有效的 URL,將返回一個新的 URL 對象。否則,將返回一個錯誤。
const url = new URL('../cats', 'http://www.example.com/dogs'); console.log(url.hostname); // "www.example.com" console.log(url.pathname); // "/cats"
在控制臺得到的 URL 對象:
當傳遞一個無效的 URL 字符串:
const exampleUrl = new URL('example'); console.log(exampleUrl);
字符串 'example' 不是一個有效的 URL。因此,會報錯 TypeError:
1.1 使用 URL 構造函數(shù)創(chuàng)建一個 URL 驗證器函數(shù)
使用 URL 構造函數(shù)和 try...catch 語句,創(chuàng)建一個函數(shù):
function isValidUrl(string) { try { new URL(string); return true; } catch (err) { return false; } }
如果參數(shù)是一個有效的 URL 時,isValidUrl 函數(shù)返回 true。否則,返回 false:
console.log(isValidUrl('https://www.example.com/')); // true console.log(isValidUrl('mailto://mail@example.com')); // true console.log(isValidUrl('freecodecamp')); // false
瀏覽器兼容性:
大部分瀏覽器都支持的
1.2 使用 URL 構造器只驗證 HTTP URL
要檢查url是否是一個有效的 HTTP URL,不要其他有效的 URL,如 'mailto://mail@example.com'。
要檢查url是否是一個有效的 HTTP URL,可以使用 URL 對象的 protocol 屬性:
function isValidHttpUrl(string) { try { const newUrl = new URL(string); return newUrl.protocol === 'http:' || newUrl.protocol === 'https:'; } catch (err) { return false; } } console.log(isValidUrl('https://www.example.com/')); // true console.log(isValidUrl('mailto://mail@example.com')); // false console.log(isValidUrl('freecodecamp')); // false
2.使用 npm 包來驗證 URL
NPM 包:is-url
和 is-url-http
2.1 使用 is-url 包驗證 URL
使用 is-url
包來檢查一個字符串是否是一個有效的 URL。這個包并不檢查傳遞給它的 URL 的協(xié)議。
安裝:
npm install is-url --save
使用:
import isUrl from 'is-url'; const firstCheck = isUrl('https://kikobeats.com'); // true const secondCheck = isUrl('mailto://kiko@beats.com'); // true const thirdCheck = isUrl('example'); // false
2.2 使用 is-url-http 包來驗證 HTTP URL
安裝:
npm install is-url-http --save
使用:
import isUrlHttp from 'is-url-http'; isUrlHttp('https://kikobeats.com') // ==> true isUrlHttp('https://kikobeats.com') // ==> true isUrlHttp('mailto://kiko@beats.com') // ==> false isUrlHttp('callto:192.168.103.77+type=ip') // ==> false
3.使用 Regex 來驗證 URL
使用正則表達式來檢查一個url是否是有效的 URL
所有有效的 URL 都遵循一個特定的模式。它們有三個主要部分,分別是:
協(xié)議
域名(或 IP 地址)
端口和路徑
有時路徑后面是一個查詢字符串或片段定位符。
3.1 使用正則驗證 URL
function isValidUrl(str) { const pattern = new RegExp( '^([a-zA-Z]+:\\/\\/)?' + // protocol '((([a-z\\d]([a-z\\d-]*[a-z\\d])*)\\.)+[a-z]{2,}|' + // domain name '((\\d{1,3}\\.){3}\\d{1,3}))' + // OR IP (v4) address '(\\:\\d+)?(\\/[-a-z\\d%_.~+]*)*' + // port and path '(\\?[;&a-z\\d%_.~+=-]*)?' + // query string '(\\#[-a-z\\d_]*)?$', // fragment locator 'i' ); return pattern.test(str); } console.log(isValidUrl('https://www.kikobeats.com/')); // true console.log(isValidUrl('mailto://kikobeats.com')); // true console.log(isValidUrl('example')); // false
3.2 使用正則驗證 HTTP URL
要使用正則來檢查一個url是否是有效的 HTTP URL,需要使用協(xié)議檢查。
使用 '^(https?:\/\/)?',而不是 ^([a-zA-Z]+:\/\/)?:
function isValidHttpUrl(str) { const pattern = new RegExp( '^(https?:\\/\\/)?' + // protocol '((([a-z\\d]([a-z\\d-]*[a-z\\d])*)\\.)+[a-z]{2,}|' + // domain name '((\\d{1,3}\\.){3}\\d{1,3}))' + // OR ip (v4) address '(\\:\\d+)?(\\/[-a-z\\d%_.~+]*)*' + // port and path '(\\?[;&a-z\\d%_.~+=-]*)?' + // query string '(\\#[-a-z\\d_]*)?$', // fragment locator 'i' ); return pattern.test(str); } console.log(isValidUrl('https://www.kikobeats.com/')); // true console.log(isValidUrl('mailto://kikobeats.com')); // false console.log(isValidUrl('example')); // false
到此這篇關于JavaScript驗證一個url的方法總結的文章就介紹到這了,更多相關JavaScript驗證url內容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關文章希望大家以后多多支持腳本之家!
相關文章
微信小程序MUI導航欄透明漸變功能示例(通過改變rgba的a值實現(xiàn))
這篇文章主要介紹了微信小程序MUI導航欄透明漸變功能,結合實例形式分析了通過改變rgba的a值實現(xiàn)透明度漸變功能的相關操作技巧,需要的朋友可以參考下2019-01-01javascript?中動畫制作方法?animate()屬性
這篇文章主要介紹了javascript?中動畫制作方法?animate()屬性,animate是所有dom元素都有的方法,可以用來最做過度動畫,關鍵幀動畫,下面文章的相關介紹需要的小伙伴可以參考一下2022-04-04javascript 導出數(shù)據(jù)到Excel(處理table中的元素)
最近做的項目中有個要求,需要將數(shù)據(jù)導出到Excel中,關于這個就不是什么問題,網(wǎng)上的資料很多。可當Table中有Input(text)之類的元素是怎么辦?2009-12-12教你巧用?import.meta?實現(xiàn)熱更新的問題
import.meta?是一個給?JavaScript?模塊暴露特定上下文的元數(shù)據(jù)屬性的對象,它包含了這個模塊的信息,這篇文章主要介紹了巧用?import.meta?實現(xiàn)熱更新的問題,需要的朋友可以參考下2022-04-04