# 正規表達 https://transbiz.com.tw/regex-regular-expression-ga-%E6%AD%A3%E8%A6%8F%E8%A1%A8%E7%A4%BA%E5%BC%8F/#8_%E3%80%90%E6%8B%AC%E8%99%9F%E3%80%91 https://doc.houdunren.com/php/5%20%E6%AD%A3%E5%88%99%E8%A1%A8%E8%BE%BE%E5%BC%8F.html#%E5%85%83%E5%AD%97%E7%AC%A6 ### 圖像 https://regexper.com/ ### 影片 https://www.youtube.com/watch?v=xrMH9uMNGt8 ### 好用文章 https://kopu.chat/2017/01/18/%E6%96%87%E6%9C%AC%E5%88%86%E6%9E%90%E5%9F%BA%E7%A4%8E-%E6%AD%A3%E8%A6%8F%E8%A1%A8%E9%81%94%E5%BC%8Fregexp/ ### 一些重點 -- 空白也是字元 -- gi G是全部 i是大小寫 ##### 特殊字元前面用反斜線 /if\(true/ \ ##### 任意字元用點 /a.man/ . ##### 多字元用[] 這算一個字元 /[asd]/會找asd [] ##### 多字元範圍 /[a-zA-z]/ [-] ##### 多字元 找不要的 不包含的 /[^a]/ 找不包含a的 [^] ##### 多字元縮寫 \d -digit [0-9] \w -word [A-Za-z0-9_]注意包含底線喔 \s -space [\n\r\t] 空白縮排之類的 相反的 \D -non-digit [^\d] \W -non-word [^\w]注意包含底線喔 \S -non-space [^\s] 空白縮排之類的 ex /\we/ 找任意+上e的 /\d\d/ 找任意兩位數 相反 /\D/ 1+2 = 3 找到+ = /\W/ i'm 20 years 找到 ' ##### 出現次數 星號 * 任意次數 加號 + 至少一次 問號 ? 0或一次 ##### 限制次數 {次數} {最少次數, 最多次數} ##### 頭尾 這樣不會全部抓 只要抓頭或尾 ^ 開頭 $ 結尾 ex /^He.*llo$/ .* 任意字元任意次數 所以下面空白才能被抓 找到 Hello Hello #### 或 | 是以單字不是字元 /and|android/ 只要前面有有找到 後面就不會去找了 #### 常用例子 /^[1-9]\d{3}-\d{2}\d{2}$/ 西元生日 "1996-08-06" /^[A-Z]\d{9}$/ 身分證字號 "A123456789" /^\w+@gmail\.com$/ gmial信箱 "test@gmail.com" 注意.是特殊字元 /^[\d\+\-\*\/]*$/ 四則運算式 "1+6/3-2" #### 只能單個單字 /b/b This is a apple /\bis\b/ 可以抓到is 單個 #### /m 找開頭 #### match 跟 replace   ###### tags: `正規表達式`
×
Sign in
Email
Password
Forgot password
or
By clicking below, you agree to our
terms of service
.
Sign in via Facebook
Sign in via Twitter
Sign in via GitHub
Sign in via Dropbox
Sign in with Wallet
Wallet (
)
Connect another wallet
New to HackMD?
Sign up