## 面試題html
校驗用戶名錶單,長度爲8-10位的只包含數字和字母的字符串,用JavaScript實現一個校驗函數。面試
/^(?=.*\d.*)(?=.*[a-zA-Z].*)[0-9a-zA-Z]{8,10}$/.test(str)
(?=)
表達正向先行斷言,知足條件的其餘匹配結果才爲真,即括號內的表達式匹配整個匹配結果才爲真/^(?!\d+$)(?![a-zA-Z]+$)[0-9a-zA-Z]{8,10}$/.test(str)
(?!)
表達負向先行斷言,知足非條件的其餘匹配結果才爲真,即括號內的表達式不匹配整個匹配結果才爲真!/^\d+$/.test(str) && !/^[a-zA-Z]+$/.test(str) && /^[0-9a-zA-Z]{8,10}$/.test(str)
//考慮記不住ASCII碼 var rangeChars = '09azAZ'; var char0Code = rangeChars.charCodeAt(0), char9Code = rangeChars.charCodeAt(1), charaCode = rangeChars.charCodeAt(2), charzCode = rangeChars.charCodeAt(3), charACode = rangeChars.charCodeAt(4), charZCode = rangeChars.charCodeAt(5); Array.from(str).every(char => { return '0' <= char && char <= '9' || 'a' <= char && char <= 'z' || 'A' <= char && char <= 'Z' }); Array.from(str).some(char => { return '0' <= char && char <= '9' }); Array.from(str).some(char => { return 'a' <= char && char <= 'z' || 'A' <= char && char <= 'Z' }); 8 <= str.length && str.length <= 10
export const validationUtil = { isNameValid:(str) => { //調用isNameValid 的同時,不該該有判斷undefind,判斷null的過程,表單取出來的不會有這倆值 str += ''; str = str.trim(); return /^(?!\d+$)(?![a-zA-Z]+$)[0-9a-zA-Z]{8,10}$/.test(str); } }
基礎正則表達式
進階express
分析調試工具函數
可視化分析工具