Remove characters in variable

0

I have a variable with the following content 00:00:01. I want to take the colon to have as final result 000001. I use the split but the result I have is 00,00,01.

var res = min_time.split(':');
    
asked by anonymous 07.01.2015 / 12:23

3 answers

5

Use the replace () function, which overrides string . The first parameter is the string that should be replaced and the second parameter is what will replace the first one. The first parameter accepts both a regular expression as a string . Example:

var teste = "00:00:01";
var teste2 = teste.replace(/:/g, "");
alert(teste2);

Remembering that using string will replace only the first occurrence. Example:

var teste = "00:00:01";
var teste2 = teste.replace(":", "");
alert(teste2);
    
07.01.2015 / 12:28
2

Then, when you use the split command, it breaks the "word" and transforms it into an array, every section that it finds separated by the : delimiter it stores in an array space if you want to continue using -o that way, you just need to do the following:

hora = res[2]+''+res[1]+''+res[0];

Important detail: JavaScript is quite annoying when typing objects, if you just do something like this:

res[2]+res[1]+res[0];

Instead of considering as a string and concatenating to get the expected result, it will add up the parts, because by 'reading' a number, it will interpret as an object of type number , so there is need the single quotes ...

Note: I recommend using the command replace , as @Earendul has passed;

    
07.01.2015 / 12:31
2

By default, the .toString () of an array will use the comma as a delimiter, you can also do the following:

var res = min_time.split(':').join('');

Take a look at this link. join

    
07.01.2015 / 12:36