Just starting with js, decided to convert Friendfeed to a fluid app, and as part of that I need to be able to parse some numbers out of a string.
How do I complete this function?
function numMessages(text) {
MAGIC HAPPENS (POSSIBLY THE DARK ART OF THE REGEX)
return number;
}
input would be "Direct Messages (15)"
output would be 15.
Instincts tell me to find the first bracket then find the last bracket, and get the text in between but I don't know how to do that. Second instinct tells me to regex for [0-9], but I don't know how to run regexes in js. Jquery is avaliable already if needed.
Thanks!
-
Is the message (aside from the number) always the same? Will it always be the same? What kind of format changes can you imagine happening in the future?Nosredna– Nosredna2009年06月29日 17:31:05 +00:00Commented Jun 29, 2009 at 17:31
-
For now I think that it will be the same, or be redesigned and totally break, so I'm happy with the naive assumption that this is the format.Rich Bradshaw– Rich Bradshaw2009年06月29日 19:02:37 +00:00Commented Jun 29, 2009 at 19:02
3 Answers 3
This should do it:
>>> 'Direct Messages (15)'.match(/[0-9]+/g);
["15"]
Just be careful if you expect more than 1 number to be in the string:
>>> 'Direct 19 Messages (15)'.match(/[0-9]+/g);
["19", "15"]
If you only wanted the first match, you could remove the g flag:
>>> 'Direct 19 Messages (15)'.match(/[0-9]+/);
["19"]
If you only wanted to match what's between the parentheses
>>> 'Direct 19 Messages (15)'.match(/\((.*?)\)/);
["(15)","15"]
// first index will always be entire match, 2nd index will be captured match
As pointed out in the comments, to get the last match:
>>> var matches = 'Direct 19 Messages (15)'.match(/[0-9]+/g);
>>> matches[matches.length-1];
"15"
Though some boundary checking would also be appropriate. :)
6 Comments
var reg = new RegExp('[0-9]+');
reg.exec('Direct Messages (15)');
Comments
function numMessages(text) {
return text.match(/\d+/g);
}
This will return all numbers (\d is a special character class equivalent to [0-9]) from the string. the /g makes the regex engine do a global search, thereby returning an array of all matches; if you just want one, remove the /g. Regardless of if your expression is global or not, match returns an array, so you will need to use array notation to get at the element you want.
Note that results from a regular expression match are of type string; if you want numbers, you can use parseInt to convert "15" to 15.
Putting that all together, if you just want one number, as it seems to appear from your initial question text:
function numMessages(text) {
return parseInt(text.match(/\d+/)[0]);
}
str = "Direct Messages (15)";
numMessages(str); // 15