Problem removing elements from an array

1

Hi, I've been trying to do something to remove the elements from an array, and I still can not try it for about 2 hours ...

This is a string that is entered by an input text (always of the text form [something], that something can be numbers or letters whatever, that later I transform it into an array with split () and I look for where the first square bracket begins and the second one, then I leave the index of the first square bracket to the index of the second one and add it 1 so that when I delete them with splice, all the elements are deleted between the square brackets including these ones, I commented the code that does not work, I mean the splice, please help!

function rec(nombre) {

//var d = nombre.value;
var get = document.getElementById('nack').value;
var usuario = get; // string
 var a = usuario.split(''); // Transformo a array
 var b = a.indexOf('[') + 1; // busco el indice del primer corchete
 var c = a.indexOf(']'); // busco el indice del segundo corchete
 var indices = (c - b) +1; // operacion para sacar los indices
 



 for ( b;b< c; b++) {
       
   document.write(parseInt(a[b]));
  

 // a[b].splice(b, c); <----- No me funciona

   

  
 }
 }
<!DOCTYPE html>
<html>
<head>
<title>Page Title</title>
</head>
<body>

<form>
<input type="text" id="nack" placeHolder="nickname"/>
<input type="button" onclick="rec(this.value);"/>
</form>
</body>
</html>

EDITO: I have also done it in another way, which is this:

function rec(nombre) {

var d = nombre.value;
var get = document.getElementById('nack').value;
var usuario = get; // string
 var a = usuario.split(''); // Transformo a array
 var b = a.indexOf('['); // busco el indice del primer corchete
 var c = a.indexOf(']'); // busco el indice del segundo corchete
 if (b == -1 && c == -1) {
 
  var indices = (c - b); // operacion para sacar los indices
 }
else {
var indices = (c - b) +1; // operacion para sacar los indices

}




var fr = a[indices];
 var eliminar = a.splice(b, indices); // elimino
 var volver = a.join(''); // vuelvo a convertir en string*/

usuario = volver;
document.write(usuario);
}
<!DOCTYPE html>
<html>
<head>
<title>Page Title</title>
</head>
<body>
<script>

</script>
<form>
<input type="text" id="nack" placeHolder="nickname"/>
<input type="button" onclick="rec(this.value);"/>
</form>
</body>
</html>

The problem is that I need to delete only the brackets inside that have numbers and then I know how to do it, I tried with typeof a[b] == 'number' and neither, and that's why I think the second one form does not help me in this, since you need a for to go through them, but I do not have any ideas left!

POSTDATA: FOR EXAMPLE IF THERE ARE MORE THAN 1 PARENTESIS WITH NUMEROS OSEA: player [234] [42], which can also eliminate both values and if there are 2 or more but one of them is string, only those with numbers are deleted ie: player [hi] [42], only 42 would be deleted please if anyone knows to help me !!

EXAMPLES FOR BETTER UNDERSTANDING:

1)

 Voy y me coloco el nombre "Pepe [soy pro] [42]"

el juego automáticamente me elimina el '[42]' dejando como nombre solamente "Pepe [soy pro]"

2) I go and put the name "Pepe [17] [42]"

el juego automáticamente me elimina el '[42]' y el '[17'] dejando como nombre solamente "Pepe"
    
asked by Eduardo Sebastian 27.04.2017 в 17:45
source

1 answer

3

You can use regular expressions:

function rec(nombre) {
  var re = /(\[\d+\])/g;
  var usuario = document.getElementById('nack').value;
  console.log(usuario);
  var result = usuario.replace(re, '');

  document.write(result);
}
<!DOCTYPE html>
<html>
<head>
<title>Page Title</title>
</head>
<body>
<script>

</script>
<form>
<input type="text" id="nack" placeHolder="nickname"/>
<input type="button" onclick="rec(this.value);"/>
</form>
</body>
</html>
    
answered by 27.04.2017 / 18:08
source