通过 name 获取 url query 参数
const getQueryByName = (name) => {const queryNameRegex = new RegExp(`[?&]${name}=([^&]*)(&|$)`)
const queryNameMatch = window.location.search.match(queryNameRegex)
// 个别都会通过 decodeURIComponent 解码解决
return queryNameMatch ? decodeURIComponent(queryNameMatch[1]) : ''
}
将字符串首字母转化为大写,剩下为小写
const capitalize = (string) => {const capitalizeRegex = /(?:^|\s+)\w/g
return string.toLowerCase().replace(capitalizeRegex, (match) => match.toUpperCase())
}
将字符串驼峰化
const camelCase = (string) => {const camelCaseRegex = /[-_\s]+(.)?/g
return string.replace(camelCaseRegex, (match, char) => {return char ? char.toUpperCase() : ''
})
}
常见手机号解决
手机号码两头四位数字用 * 示意
const phone = '133123456789'
const phoneReg = /^(\d{3})(\d{4})(\d{4})$/
const sphone = phone.replace(phoneReg, '$1--$3')