5
As input by 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 to come out:
[
[1, 2],
[3, 4],
[5, 6]
]
I’m in doubt on how to get this output in Javascript.
Code so far in Jsfiddle (is giving infinite loop): http://jsfiddle.net/cbLs8/7/
<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");
}
});
How about
/((\d+)-(\d+))+/
?– Evandro Silva
Is this what you want?: http://jsfiddle.net/cbLs8/8/
– Sergio