let str = 'qweqweqweqweqw3@qq.net我我';
function getInfo(str) {
try {
const payInfo = matchInfo(str);
log('payInfo1' + payInfo)
return payInfo
} catch (error) {
console.log(error);
}
}
//解析账号信息字符串 返回一个对象
function matchInfo(str) {
const nameRegex = /[\u4E00-\u9FFF]{2,4}/;
const emailRegex = /\w+(-+.\w+)*@\w+(-.\w+)*.\w+(-.\w+)*/g;
const accountRegex = /\d{11}/g;
const nameMatch = str.match(nameRegex);
const accountMatch = str.includes('@') ? str.match(emailRegex) : str.match(accountRegex);
// if (nameMatch === null) {
// throw new Error('未获取到名字');
// }
// if (accountMatch === null) {
// throw new Error('未获取到账号');
// }
const info = {
name: nameMatch[0] || '',
account: accountMatch[0] || ''
};
log(info)
return info;
}
// getInfo(str);
function log(a) {
return console.log(a)
}
const name = document.querySelector('.name')
const account = document.querySelector('.account')
const input = document.querySelector('.input')
// input.addEventListener("focus", function () {
// (async () => {
// try {
// const text = await navigator.clipboard.readText();
// str = text
// input.value = text
// let info = getInfo(str)
// // console.log('info' + info.name);
// name.value = info.name
// account.value = info.account
// } catch (e) {
// console.log(e.message)
// }
// })();
// })
input.addEventListener("input", function () {
str = input.value
let info = getInfo(str)
if(info.name){
}
console.log('info' + info.name);
name.value = info.name
account.value = info.account
// try {
// } catch (e) {
// // console.log(e.message)
// if (e.message == '未获取到名字') {
// log('@@@')
// console.log(e.message)
// // name.value = ''
// } else if (e.message == '未获取到账号') {
// log('###')
// console.log(e.message)
// // account.value = ''
// }
// }
})
姓名:<input class="name" type="text"> <br>
账号:<input class="account" type="text">
<input class="input" type="text">
.input{
width: 300px;
height: 100px;
}