Create RegExp
You have not shown how you create a Javascript regular expression, for example, do you use a letter:
var rex = /([a-z]+)((\d+)([a-z]+))?,?/;
or string
var rex = new RegExp("([a-z]+)((\\d+)([a-z]+))?,?");
, , .
Javascript , . g, :
var rex = /([a-z]+)((\d+)([a-z]+))?,?/g;
var rex = new RegExp("([a-z]+)((\\d+)([a-z]+))?,?", "g");
RegExp#exec, String#match
, String#match . , String#match ( RegExp#exec, ). String#match , ... , . RegExp#exec , .
, :
var rex, str, match, index;
rex = /([a-z]+)((\d+)([a-z]+))?,?/g;
str = "water2cups,flour4cups,salt2teaspoon";
rex.lastIndex = 0;
while (match = rex.exec(str)) {
log("Matched:");
for (index = 0; index < match.length; ++index) {
log(" match[" + index + "]: |" + match[index] + "|");
}
}
( log div.)
:
Matched:
match[0]: |water2cups,|
match[1]: |water|
match[2]: |2cups|
match[3]: |2|
match[4]: |cups|
Matched:
match[0]: |flour4cups,|
match[1]: |flour|
match[2]: |4cups|
match[3]: |4|
match[4]: |cups|
Matched:
match[0]: |salt2teaspoon|
match[1]: |salt|
match[2]: |2teaspoon|
match[3]: |2|
match[4]: |teaspoon|
(, Javascript match[0] , match[1] .. .)