假设我有一串文本,例如:
“您好,您的工作密码是 931234。同样,号码是 931234。”
如何解析找到的前6 位数字?(在这种情况下,931234)
var msg = "Hello, your work pin number is 931234. Again, the number is 931234";
(msg.match(/\d{6}/) || [false])[0]; // "931234"
如果没有找到 6 位出现,则语句将返回false
一种精确 6 位数字的方法;
var s = "Hello, your work pin number is 931234. Again, the number is 931234."
//start or not a digit, 6 digits, not a digit or end
result = s.match(/(^|[^\d])(\d{6})([^\d]|$)/);
if (result !== null)
alert(result[2]);
var expr = /(\d{6})/;
var digits = expr.match(input)[0];
var text = "Hello, your work pin number is 931234. Again, the number is 931234.".replace(new RegExp("[^0-9]", 'g'), '');
alert(text.substring(0,6));