一、網址正則表達式語法
正則表達式是處理字符串的強大工具,它是一個特殊的字符串序列,可以幫助我們快速、準確地匹配、驗證和提取所需的信息。在編寫匹配網址的正則表達式時,需要注意以下語法:
- URL格式:protocol://hostname[:port]/path/[?query-string][#anchor]
- 正則表達式:/(protocol|http|https|ftp):\/\/(-\.)?([^\s/?.#-]+\.?)+([^.\s]+)(\/[^?\s]*)*\/?([?][^\s]*)?(\#[^\s]*)?/i
其中,i標誌表示不區分大小寫、/表示正則表達式開始和結束的符號、|表示或、()用於捕獲分組、.表示匹配除換行符外的任意字符、*表示之前的字符重複0次或多次、+表示之前的字符至少重複一次。
二、網址的正則表達式舉例
以下是一些匹配網址正則表達式的例子:
- 匹配http和https協議的URL:
/(https?|ftp):\/\/(-\.)?([^\s/?.#-]+\.?)+([^.\s]+)(\/[^?\s]*)?(\?[^\s]*)?/i
- 匹配帶有查詢字符串的URL:
/^http:\/\/[a-z0-9-]+(.[a-z0-9-]+)*(\?[a-z0-9+&$_.-]+[=](.*?)(&[a-z0-9+&$_.-]+[=](.*?))*)?$/i
- 匹配email地址:
/\b[A-Za-z0-9._%+-]+@[A-Za-z0-9.-]+\.[A-Z]{2,}\b/i
三、網址正則表達式匹配
在JavaScript中,可以使用RegExp對象中的exec()方法進行正則表達式匹配,也可以使用String對象中的match()方法進行匹配。
// 使用exec()方法 const regex = /(https?|ftp):\/\/(-\.)?([^\s/?.#-]+\.?)+([^.\s]+)(\/[^?\s]*)?(\?[^\s]*)?/i; const url = 'https://www.example.com/index.html?q=search'; const matches = regex.exec(url); console.log(matches); //["https://www.example.com/index.html?q=search", "https", undefined, "www.", "example.com", "/index.html", "?q=search", undefined] // 使用match()方法 const url = 'https://www.example.com/index.html?q=search'; const regex = /(https?|ftp):\/\/(-\.)?([^\s/?.#-]+\.?)+([^.\s]+)(\/[^?\s]*)?(\?[^\s]*)?/i; const matches = url.match(regex); console.log(matches); //["https://www.example.com/index.html?q=search", "https", undefined, "www.", "example.com", "/index.html", "?q=search", undefined]
四、sql正則表達式
在SQL中,可以使用LIKE運算符和通配符進行模糊匹配,也可以使用REGEXP運算符和正則表達式進行高級匹配。
以下是一些SQL中常用的正則表達式:
- 匹配以‘apple’開頭的字符串:
SELECT * FROM fruits WHERE name LIKE 'apple%';
- 匹配長度為8的全數字字符串:
SELECT * FROM users WHERE phone REGEXP '^[0-9]{8}$';
- 匹配以字母、數字或_開頭的用戶名:
SELECT * FROM users WHERE username REGEXP '^[a-zA-Z0-9_]';
五、網址的正則表達式怎麼寫
編寫匹配網址的正則表達式需要考慮網址的各部分,包括協議、域名、路徑、查詢字符串和錨點等。以下是一種簡單的寫法:
/(https?|ftp):\/\/[-\w.]+(:\d+)?(\/([\w\/_.]*)?)?(\?\S+)?(#\S+)?/
上述正則表達式可以匹配以下URL:
- https://www.example.com/
- http://subdomain.example.com/path/file.html
- ftp://ftp.example.com/
- https://www.example.com/index.html?q=search
六、正則表達式測試網址
為了方便編寫和測試正則表達式,可以使用一些在線工具,如RegExr、Regex101、RegExp Tester等。
- RegExr:https://regexr.com/
- Regex101:https://regex101.com/
- RegExp Tester:https://www.regexpal.com/
七、正則表達式網址
以下是一些關於正則表達式和匹配網址的網絡資源和參考書籍:
- MDN Web文檔:https://developer.mozilla.org/zh-CN/docs/Web/JavaScript/Guide/Regular_Expressions
- 《精通正則表達式》(第三版) 作者:Jeffrey E.F. Friedl
- 《正則表達式必知必會》(第三版) 作者:Ben Forta
八、正則表達式提取網址
在JavaScript中,可以使用正則表達式的分組功能和exec()方法提取網址的各個部分:
const url = 'https://www.example.com/index.html?q=search' const regex = /(https?|ftp):\/\/(-\.)?([^\s/?.#-]+\.?)+([^.\s]+)(\/[^?\s]*)?(\?[^\s]*)?/i; const matches = regex.exec(url); const [fullUrl, protocol, , hostname, , path, queryString, anchor] = matches; const result = { protocol, hostname, path, queryString, anchor }; console.log(result); // { protocol: "https", hostname: "www.example.com", path: "/index.html", queryString: "?q=search", anchor: undefined }
九、在線正則表達式工具
以下是一些常用的在線正則表達式工具:
- RegExr:https://regexr.com/
- Regex101:https://regex101.com/
- RegExp Tester:https://www.regexpal.com/
在編寫正則表達式時,建議使用這些工具進行調試和測試,以確保正則表達式可以正確匹配、提取所需的內容。
原創文章,作者:小藍,如若轉載,請註明出處:https://www.506064.com/zh-hant/n/150389.html