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
4 Answers
Reset to default 14var 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));