Alex Rivera | Logout

Javascript - Regex access multiple occurrences

Asked 2011-09-02T07:50:49.147
26

I have this text

txt = "Local residents o1__have called g__in o22__with reports...";

in which I need to get the list of numbers between each o and __

If I do

txt.match(/o([0-9]+)__/g);

I will get

["o1__", "o22__"]

But I'd like to have

["1", "22"]

How can I do that ?

Edit
Report

1 Answer

32

See this question:

txt = "Local residents o1__have called g__in o22__with reports...";
var regex = /o([0-9]+)__/g
var matches = [];
var match = regex.exec(txt);
while (match != null) {
    matches.push(match[1]);
    match = regex.exec(txt);
}
alert(matches);
answered 2011-09-02T07:57:20.253

Your Answer