訴求:9開頭的十位數或者是+7,+8開頭的11位數
代碼實現
let testTeg1 = /^\+[7|8]\S{0,11}$/ // +7 +8開頭 後面可以跟上0 ~ 11位的值
let testTeg2 = /^9\d{0,10}$/ // 9 開頭 後面可以跟上0 ~ 10位的值
let test3 = /^(\+[7|8]\d{0,11})|(9\d{0,10})$/
測試
testTeg1.test('+7') // true
testTeg1.test('+8') // true
testTeg1.test('+700') // true
test3 .test('+7') // true
test3 .test('90') // true
testTeg2.test(900) // true
正則分析
\+[7|8]
:以+7 +8開頭\d
:任意數字\S
:任意字符{0,11}
:0 ~ 11位長度的字符/^(\+[7|8]\d{0,11})|(9\d{0,10})$/
:當前的數據是否滿足其中一個正則表達式