How to remove the last space in each line from a textarea?

Asked

Viewed 180 times

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>

4 answers

2

If you want to exchange "everything that is not number" for space, but preserving line breaks, it is simpler to do so:

window.addEventListener('DOMContentLoaded', (event) => {
    let textarea = document.getElementById('texto');
    textarea.value = textarea.value.replace(/[^\d\n\r]/g, ' ');
});
<form>
  <textarea id="texto" name="texto" rows="10" cols="30">
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</textarea>
</form>

In this case, the regex is [^\d\n\r]. The [^ creates a character class denied, who takes everything that nay whatever’s in the brackets.

In case, inside the brackets we have \d (one shortcut for digits from 0 to 9), in addition to \r and \n (characters used as line breaks - the \r, in case, is for Windows line breaks, if you copy and paste the text of some editor, for example).

That is, the regex replaces anything that is not number or line breaks. This way the result of the substitution does not stay with the spaces at the end (besides preserving the original lines).


In the comments you said you also want to eliminate two or more spaces. In this case, just add another replace:

window.addEventListener('DOMContentLoaded', (event) => {
    let textarea = document.getElementById('texto');
    textarea.value = textarea.value.replace(/[^\d\n\r]/g, ' ').replace(/ {2,}/g, ' ');
});
<form>
  <textarea id="texto" name="texto" rows="10" cols="30">
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</textarea>
</form>

I added replace(/ {2,}/g, ' ') - notice that there is a space before the {. And in this case, the quantifier {2,} means "two or more". That is, regex replaces 2 or more spaces by just one.

  • Great! And how would you eliminate the double spaces generated by happiness? I just want a space between the numbers.

  • @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.

1

Have you tried using the .trim()? It removes all initial spaces and all endings of the sentence.
There is also the .trimRight() or the .trimEnd(), both remove only the spaces at the end of the sentence.
This does not overwrite the variable, so you have to assign the result to it again.
From what you described, it suits you.

var e = '   exemplo   ';

console.log('variavel: ' + e + '.')
console.log('trim: ' + e.trim() + '.');
console.log('trim end: ' + e.trimEnd() + '.');
console.log('trim right: ' + e.trimRight() + '.');

  • trimEnd() worked well. Thank you

1


function formataTextarea() {

  var lines = document.getElementById('text-area').value.split('\n');
  var formated = [];
  
  // Percorre todas as linhas
  lines.forEach(line => {
    // Remove o ultimo caracter da linha se for espaço
    if (line.charAt(line.length - 1) === ' ') {
      line = line.substring(0, line.length - 1);
    }
    // Substitui todos os caracteres não numeros por espaço
    formated.push(line.replace(/\D/g, ' '));
  });

  // Escreve na textarea
  document.getElementById('text-area').value = formated.join('\n');
}
<textarea id="text-area" rows="6" cols="50">
Lorem ipsum d24olor sit, 
amet consectetur adi564pisicing elit. 9
Quasi corrupti re678pellendus 1 
deserunt fugi46at! 
Odio, nesciunt 678eveniet. 
 
</textarea>
<button type="button" onclick="formataTextarea();">FORMAT</button>

0

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>

Browser other questions tagged

You are not signed in. Login or sign up in order to post.