Why doesn't this method work for assigning characters in JavaScript?

Ok, here is the beginners question:

//function removes characters and spaces that are not numeric.

// time = "2010/09/20 16:37:32.37"
function unformatTime(time) 
{       

    var temp = "xxxxxxxxxxxxxxxx";

    temp[0] = time[0];
    temp[1] = time[1];
    temp[2] = time[2];
    temp[3] = time[3];
    temp[4] = time[5];
    temp[5] = time[6];
    temp[6] = time[8];
    temp[7] = time[9];
    temp[8] = time[11];
    temp[9] = time[12];
    temp[10] = time[14];
    temp[11] = time[15];
    temp[12] = time[17];
    temp[13] = time[18];
    temp[14] = time[20];
    temp[15] = time[21];   


}

In FireBug, I see that characters from time to time are not assigned to temp? Should I use the replace () function to do something like this in JS?

Thank.

+3
source share
2 answers

[^\d] - This is a regular expression for "not numbers."

In details

[]represents a "character class" or a group of characters to match.
\dis a shortcut for 0-9or for any number.
^in a character class, negates the class.

function unformat(t)
{
   return t.replace( /[^\d]/g, '' );
}

In any case, you cannot access such a line as in one of the main browsers. You will need to use str.charAt(x).

+4

.

function unformatTime(time) {
    return time.replace(/[^\d]/g, '');
}

, , . "G" "", , .

  • ^ ""
  • \d ""
  • g ""
+3

Source: https://habr.com/ru/post/1765724/


All Articles