const str = '1353qq.netwwwwwwwwwwwwww+23145168910我我';
//解析字符串获取用户支付宝信息 返回一个对象 name为姓名,account为账号
async function getInfo() {
let payInfo = {
name: '',
account: ''
}
try {
payInfo.name=await matchName(str)
payInfo.account= await matchAccount(str)
console.log(payInfo)
} catch (e){
console.log(e)
}
}
// function getInfo(str) {
// let a = {
// name: '',
// account: ''
// }
// matchName(str)
// .then(name => {
// a.name = name
// return matchAccount(str)
// }).then(account => {
// a.account = account
// console.log(a)
// }).catch(error => {
// console.log(error)
// })
// }
getInfo(str)
// 使用正则表达式匹配名字
function matchName(str) {
const nameRegex = /[\u4E00-\u9FFF]{2,4}/;
return new Promise((resolve, reject) => {
const rs = str.match(nameRegex)
if (rs === null) {
reject('未获取到名字')
} else {
resolve(rs[0])
}
})
}
function matchAccount(str) {
//判断支付宝账号是否为邮箱
if (str.includes('@')) {
// 使用正则表达式匹配邮箱
const emailRegex = /\w+(-+.\w+)*@\w+(-.\w+)*.\w+(-.\w+)*/g;
return new Promise((resolve, reject) => {
const rs = str.match(emailRegex)
if (rs === null) {
reject('未获取到账号')
} else {
resolve(rs[0])
}
})
} else {
// 否则使用正则表达式匹配手机号码
const accountRegex = /\d{11}/g;
return new Promise((resolve, reject) => {
const rs = str.match(accountRegex)
if (rs === null) {
reject('未获取到账号')
} else {
resolve(rs[0])
}
})
}
}
// console.log('名字:', name);
// console.log('手机号码:', phone);
// console.log('邮箱:', email);
console