I’m not able to get an array from the result of a regex match:
var txt = '[Eig2]=>100 [Eig1]=="test"';
var reg = '(\\[)((?:[a-z][a-z]+))(\\d+)(\\])';
var m = txt.match(new RegExp(reg, ["i"]));
if (m != null) {
for (var i = 0; i < m.length; i++) {
console.log(m[i]);
}
} else {
console.log("null");
}
What it returns:
[Eig2]
[
Eig
2
]
What I want:
[Eig2]
[Eig1]
May I have to do it without “new RegExp”, but with “/([)((?:[a-z][a-z]+))(\d+)(])/g” it does not work…
Some ideas?
Regards
First, I would simplify the expression:
I’ve added the
/ifor case insensitive matching and/gmodifier to match multiple occurrences. Then you call it like this:To extract the first memory capture:
Granted,
.replace()is being abused hereThen, evaluate captures:
Update
A somewhat friendlier way to build the array of memory captures: