views:

65

answers:

1

I need to get all the numbers that start with #.

"this is sentence 1 . Item number #4567 "

"this is sentence 2. Item number #8937 and #6723"

I am using JavaScript.

Using regular expression how do I get all the numbers in a string.

+9  A: 
var matches = "Item number #8937 and #6723".match(/#\d+/g);
print(matches[0]);   // #8937
print(matches[1]);   // #6723
Chris Jester-Young
I would vote the answer up, but it's too easy to deserve a vote I think.
darkporter
@darkporter: That's perfectly good with me. Your vocal "vote" of confidence is very much appreciated. :-)
Chris Jester-Young
I can have 1 or 10 such numbers. It is not limited to 2.
Nadal
@dorelal: It will work with any number of numbers.
Matthew Crumley