simple regex /:[a-z]+/ not working as expected in javascript

javascript, regex

Solution

Using RegExp.`exec()` with `g` (global) modifier is meant to be used inside a loop for getting all matches.

var str = '/a/:b/c/:d'
var re  = /:[a-z]+/g
var matches;

while (matches = re.exec(str)) {
   // In array form, match is now your next match..
}

You can also use the String.`match()` method here.

var s = '/a/:b/c/:d',
    m = s.match(/:[a-z]+/g);

console.log(m); //=> [ ':b', ':d' ]

Problem

Below is a very simple regex code, which works correctly in php and ruby, but not in JS. Plead help me get it working: ``` var r = /:[a-z]+/ var s = '/a/:b/c/:d' var m = r.exec(s) // now m is [":b"] // it should be [":b", ":d"] // because that's what i get in ruby and php ```

Original source