最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

regex - In Javascript, how can I find and parse out a 6 digit number? - Stack Overflow

programmeradmin8浏览0评论

Let's say I have a string of text like:

"Hello, your work pin number is 931234. Again, the number is 931234."

How do I parse out the first 6 digit number that I find? (in this case, 931234)

Let's say I have a string of text like:

"Hello, your work pin number is 931234. Again, the number is 931234."

How do I parse out the first 6 digit number that I find? (in this case, 931234)

Share Improve this question edited Jul 4, 2014 at 10:50 Qantas 94 Heavy 16k31 gold badges72 silver badges88 bronze badges asked Jun 14, 2013 at 10:22 TIMEXTIMEX 272k367 gold badges801 silver badges1.1k bronze badges 1
  • 1 regular-expressions.info/tutorial.html – deceze Commented Jun 14, 2013 at 10:24
Add a ment  | 

4 Answers 4

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

if no 6-digit occurence is found then the statement will return false

One way for exactly 6 digits;

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));
发布评论

评论列表(0)

  1. 暂无评论