Created
September 8, 2018 01:29
-
-
Save ClausClaus/a9af8ecdd144a7cc37f257b7b4710bd5 to your computer and use it in GitHub Desktop.
将手机号格式化为以横杠分隔字符串
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
let phoneReg = /^134[0-8]\d{7}$|^13[^4]\d{8}$|^14[5-9]\d{8}$|^15[^4]\d{8}$|^16[6]\d{8}$|^17[0-8]\d{8}$|^18[\d]{9}$|^19[8,9]\d{8}$/ | |
/** | |
* 格式化手机号码 | |
* @param {*} mobileNum 手机号码 | |
*/ | |
function split(mobileNum) { | |
let value = mobileNum.replace(/\D/g, '').substring(0, 11) | |
if (value.length === 11 && phoneReg.test(value)) { | |
value = value.replace(/^(...)(....)/g, '$1-$2-') | |
} | |
return value | |
} | |
console.log(split('18666343371')) // return --> 186-6634-3371 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment