假设我有一串文字,如:
“你好,你的工作密码是931234.再次,这个数字是931234。”
如何解析我找到的第一个 6位数字? (在这种情况下,931234)
答案 0 :(得分:8)
var msg = "Hello, your work pin number is 931234. Again, the number is 931234";
(msg.match(/\d{6}/) || [false])[0]; // "931234"
如果未找到6位数字,则该语句将返回false
答案 1 :(得分: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]);
答案 2 :(得分:1)
var expr = /(\d{6})/;
var digits = expr.match(input)[0];
答案 3 :(得分: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));