As input from the user I am expecting pairs of numbers separated by commas:
// se for um par só
1-2
// se for mais de um par
1-2,3-4,5-6
I expect something like this:
[
[1, 2],
[3, 4],
[5, 6]
]
I'm in doubt about getting this output in Javascript.
Code so far in JSFiddle (giving infinite loop): link
<input type="text" id="entrada" value="1-2,3-4,5-6"></input>
<button id="botao">Testar</button>
$("#botao").click(function () {
var regex = new RegExp(/(\d+)-(\d+)/);
var match;
var string = $("#entrada").val();
if (match = regex.exec(string)) {
while (match !== null) {
console.log(match);
match = regex.exec(string)
}
} else {
alert("no match");
}
});