Friday, May 17, 2024
 Popular · Latest · Hot · Upcoming
106
rated 0 times [  107] [ 1]  / answers: 1 / hits: 151724  / 14 Years ago, sat, february 12, 2011, 12:00:00
testing= testing.match(/(d{5})/g);


I'm reading a full html into variable. From the variable, want to grab out all numbers with the pattern of exactly 5 digits. No need to care of whether before/after this digit having other type of words. Just want to make sure whatever that is 5 digit numbers been grabbed out.



However, when I apply it, it not only pull out number with exactly 5 digit, number with more than 5 digits also retrieved...



I had tried putting ^ in front and $ behind, but it making result come out as null.


More From » regex

 Answers
20

I am reading a text file and want to use regex below to pull out numbers with exactly 5 digit, ignoring alphabets.



Try this...


var str = 'f 34 545 323 12345 54321 123456',
matches = str.match(/bd{5}b/g);

console.log(matches); // ["12345", "54321"]

jsFiddle.


The word boundary b is your friend here.


Update


My regex will get a number like this 12345, but not like a12345. The other answers provide great regexes if you require the latter.


[#93772] Thursday, February 10, 2011, 14 Years  [reply] [flag answer]
Only authorized users can answer the question. Please sign in first, or register a free account.
mira

Total Points: 460
Total Questions: 108
Total Answers: 99

Location: American Samoa
Member since Fri, Aug 26, 2022
2 Years ago
mira questions
;