How to implode to remove characters other than the +(plus) sign with Javascript?

Asked

Viewed 94 times

0

I have this variable: 10 + 20 + 30 + 40 + 50 = 150.

I want to know how I can use the implode() Javascript to remove anything that is in front of each number, can be any sign or special characters, this string can also have several sizes.

Desired result:

var numero = 10 + 20 + 30 + 40 + 50 = 150;

var 10 = 10
var 20 = 20
var 30 = 30
var 40 = 40
var 50 = 50

var igual = 150

My code:

var valor = "10+20+30+40+50=150";
var separar = valor.split('+');//Nesse caso fica limitado ao sinal de adição
alert(adicao);
  • the implode only transforms an array into a string separated by distinct characters, and these characters do not go together with the value saved. To remove signals, a regular expression should be used, in the case of the transformed array, using a loop.

  • Remembering q you can’t create variables starting with numbers.

  • Can be ( n_10 ) ( n_20 ) and so on

  • 1

    valor.replace('=','+').split('+') this way you will have an array with all values in the same order that are in your variable. This helps?

  • So it can be any sign or any special characters, it can be ( 10@10) / (10/10) / (10*10 ) or any other arithmetic sign or special character.

  • What are you going to do with these values? This is the most important, this is what defines what the output format needs to be.

  • or even, you can use a regex in the split, so: valor.split(/\D/)

Show 2 more comments

2 answers

4

Use a REGEX inside the split

  • In that case \D means that it will perform the "split" whenever it finds a character that is not numerical.

var valor = "10+20+30+40+50=150";
var valoresSeparados = valor.split(/\D/).map(function(item) {
    return parseInt(item, 10);
});
console.log(valoresSeparados);

  • If there are two non-numeric sequential characters your solution puts a blank item in the array, you have problems with this ai.

  • @Gabrielkatakura "in front of each number, can be any sign or special characters", implied that will have a character, and according to the example posted in the question suggests the same thing.

  • Exactly, but this is not a problem, according to AP, he wants to separate the numbers from the other characters, if they have two characters will not make sense for AP.

  • Then, it is almost that, it would have to quardar each separate number in a variable of different name and the value of the variable is an integer number without quotation marks. Example: var_10 = 10 / var_20 = 20

  • 1

    You want to create a variable for each number?? What’s the problem with using an array? By the way, how do you want the code to create variables for you? I mean, that’s even possible, but that understand what you intend.

  • Then I will manipulate the variables, and each value has to be in a separates variavels, minus the sign of ( = ) that will be in a variable called EQUAL var equal = 150

  • I used an Array.map() to convert the array items into integer, now in my opinion I think it’s best to use the values within the array.

  • abduzeedo, in the array the values are separated, I think there is no need to assign them to other variables, and you will always have at the last position of the array the result.

  • Excuse me, could someone help me suggest this topic ? I need to insert the arrays in getElementById values

  • That’s another question, you can look for como inserir valores em elementos html com javascript Right here at SOPT, you have a lot of related questions.. If you don’t find it, post a question. :)

Show 5 more comments

2

That’s what you want?

var valor = "10++-20++30+40+50";
var variaveis = "";
var separar = valor.match(/(\d+)/g);

separar.forEach(function(x, indice) {
  variaveis += "var n_" + indice + " = " + x + ";\n";
});

eval(variaveis); // executando o eval para adicionar as variaveis criadas acima

var resultado = "";

separar.forEach(function(x, indice) {
  resultado += "n_" + indice + (indice == separar.length - 1 ? "" : " + ");
}); // rodando um foreach criar a linha q irá somar as variaveis

var igual = eval(resultado);

console.log("Resultado: " + igual);

  • Hello 10.2mil, in this code provided gently by you, it separates the numbers independent of the arithmetic sign or special characters that is between the numbers ?

  • Separate only numbers. Regardless of whether the separator is + or - (less).

  • I even edited it for you to see.

  • Has to separate independent of the arithmetic sign or special character ?

  • He already does it, using the match(/(\d+)/g), he just separates the numbers.

  • cool friend, I only have one more question, I would like to display the values of the variables in a DIV, where I insert this ? Because I was in doubt as to what ( Eval ) you created that concatenates everything.

Show 2 more comments

Browser other questions tagged

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