2
I use the function below to replace all the different characters of numbers in a textarea with "spaces". The problem is that at the end of each line of the textarea always has a space. How to delete this last space and most others?
function formatatextarea(tarea) { //formata as matrizes colocando espaco entre as dezenas
var tab = RegExp("\\t", "g");
tarea.value =
tarea.value.replace(tab, ' ');
/*var espaco = RegExp(" ", "g");
tarea.value =
tarea.value.replace(espaco,' ');*/
var traco = RegExp("-", "g");
tarea.value =
tarea.value.replace(traco, ' ');
var virg = RegExp(",", "g");
tarea.value =
tarea.value.replace(virg, ' ');
var tespaco = RegExp(" ", "g");
tarea.value =
tarea.value.replace(tespaco, ' ');
var ptvirg = RegExp(";", "g");
tarea.value =
tarea.value.replace(ptvirg, ' ');
var pt = RegExp(/\./, "g");
tarea.value =
tarea.value.replace(pt, ' ');
/*var pesp = RegExp(/^\s*|\s*$/g, "g");
tarea.value =
tarea.value.replace(pesp," ");
*/
var nl = RegExp(/(\r\n\s\r\n|\n\s\n|\r\s\r)/gm, "g");
tarea.value =
tarea.value.replace(nl, "\r\n");
var nld = RegExp(/^\S.*$(\r\n\s|\n\s|\r\s)/gm, "g");
tarea.value =
tarea.value.replace(nld, "\r\n");
var last = RegExp(/\s*,\s*$/, "g");
tarea.value =
tarea.value.replace(last, "\r\n");
var special = RegExp(/[&\/\\#,+()$~%.'":*?<>{}]/g, "g");
tarea.value =
tarea.value.replace(special, " ");
var letter = RegExp(/[a-zA-Z]/g, "g");
tarea.value =
tarea.value.replace(letter, "");
/*var tdespaco = RegExp(/\s+/g, "g");
tarea.value =
tarea.value.replace(tdespaco,'\r\n');*/
if ($('#cjtsdezenas').val()[0] === " ") {
$('#cjtsdezenas').val()[0] == "";
}
if ($('#cjtsdezenas').val()[1] === " ") {
$('#cjtsdezenas').val()[1] == "";
}
var despaco = RegExp(" ", "g");
tarea.value =
tarea.value.replace(despaco, ' ');
}
Example:
Paste the matrix below into the textarea for testing:
01-02-05-06-08-14-15
01-02-07-08-10-11-14
01-02-07-09-10-13-15
01-03-04-11-12-15-16
01-05-06-07-08-14-15
02-03-04-07-11-12-16
05-06-08-09-10-13-14
function formatatextarea(tarea) { //formata as matrizes colocando espaco entre as dezenas
var tab = RegExp("\\t", "g");
tarea.value =
tarea.value.replace(tab, ' ');
/*var espaco = RegExp(" ", "g");
tarea.value =
tarea.value.replace(espaco,' ');*/
var traco = RegExp("-", "g");
tarea.value =
tarea.value.replace(traco, ' ');
var virg = RegExp(",", "g");
tarea.value =
tarea.value.replace(virg, ' ');
var tespaco = RegExp(" ", "g");
tarea.value =
tarea.value.replace(tespaco, ' ');
var ptvirg = RegExp(";", "g");
tarea.value =
tarea.value.replace(ptvirg, ' ');
var pt = RegExp(/\./, "g");
tarea.value =
tarea.value.replace(pt, ' ');
/*var pesp = RegExp(/^\s*|\s*$/g, "g");
tarea.value =
tarea.value.replace(pesp," ");
*/
var nl = RegExp(/(\r\n\s\r\n|\n\s\n|\r\s\r)/gm, "g");
tarea.value =
tarea.value.replace(nl, "\r\n");
var nld = RegExp(/^\S.*$(\r\n\s|\n\s|\r\s)/gm, "g");
tarea.value =
tarea.value.replace(nld, "\r\n");
var last = RegExp(/\s*,\s*$/, "g");
tarea.value =
tarea.value.replace(last, "\r\n");
var special = RegExp(/[&\/\\#,+()$~%.'":*?<>{}]/g, "g");
tarea.value =
tarea.value.replace(special, " ");
var letter = RegExp(/[a-zA-Z]/g, "g");
tarea.value =
tarea.value.replace(letter, "");
/*var tdespaco = RegExp(/\s+/g, "g");
tarea.value =
tarea.value.replace(tdespaco,'\r\n');*/
if ($('#cjtsdezenas').val()[0] === " ") {
$('#cjtsdezenas').val()[0] == "";
}
if ($('#cjtsdezenas').val()[1] === " ") {
$('#cjtsdezenas').val()[1] == "";
}
var despaco = RegExp(" ", "g");
tarea.value =
tarea.value.replace(despaco, ' ');
}
<textarea name="cjtsdezenas" id="cjtsdezenas" onchange="formatatextarea(this)" cols="150" rows="10" placeholder="Cole uma sequência de X Dezenas em cada linha, separadas por 1 espaço ou selecione as dezenas utilizando o seletor acima."></textarea>
Great! And how would you eliminate the double spaces generated by happiness? I just want a space between the numbers.
– user2876585
@user2876585 Using the matrix you put of example, and my code, this situation of having more than 2 spaces does not occur, but anyway I updated the answer with this case.
– hkotsubo