Use regular expressions to find out the name, phone number, and ID card in the string

String rules: name, phone number, and ID card must be separated by one or more spaces

 var target = `姓名 15112345678 500000000000000000`;
 var userName = /[\u4E00-\u9FA5A-Za-z]+/g;
 var phone = /\b1[3456789]\d{9}\b/g;
 var card = /\b(\d{6})(\d{4})(\d{2})(\d{2})(\d{3})([0-9]|X|x)\b/g;
 var res = target.match(pattern)
 var res1 = target.match(username)
 var res2 = target.match(ph)
 console.log("res",res)
 console.log("res1",res1)
 console.log("res2",res2)

insert image description here
If you need to continue parsing into an array

let arr = [];
res.forEach((ele,i)=>{
    
    
	arr.push({
    
    
	name:ele,
	phone:res1[i],
	card:res2[i]
	})
})
console.log("arr",arr);

Guess you like

Origin blog.csdn.net/weixin_43614065/article/details/125821950