-1

假设我有一串文本,例如:

“您好,您的工作密码是 931234。同样,号码是 931234。”

如何解析找到的6 位数字?(在这种情况下,931234)

4

4 回答 4

12
var msg = "Hello, your work pin number is 931234. Again, the number is 931234";
(msg.match(/\d{6}/) || [false])[0]; // "931234" 

如果没有找到 6 位出现,则语句将返回false

于 2013-06-14T10:25:20.270 回答
3

一种精确 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]);
于 2013-06-14T10:35:05.230 回答
1
var expr = /(\d{6})/;
var digits = expr.match(input)[0];
于 2013-06-14T10:27:42.317 回答
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));
于 2013-06-14T10:26:16.020 回答