views:

43

answers:

2

Ok, here goes a newbie 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 can see that the characters from time are not assigned to temp? Do i have to use a replace() function to do something like this in JS?

Thank You.

+4  A: 

[^\d] is the regular expression for "not digit".

In more detail,

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

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

You can't access a string like that in one of the major browsers, anyway. You would need to use str.charAt(x).

Stefan Kendall
@Stefan, just because I still regard regex as being a dark art, could you talk me through what the `/[^\d]/, ''` does? O_o I suspect the OP, given his initial approach that made you cry, would probably appreciate this too...+1
David Thomas
I broke it down a bit more.
Stefan Kendall
Much appreciated ^_^
David Thomas
Thanks guys and now I know more about how string work in JS.
Tommy
@Stefan note that plain "\D" (no brackets) also means "not a digit".
Pointy
+1 for the `str.charAt(x)` correction.
Peter Ajtai
Oh, and to be able to pass in any number of times and get an array of unformatted times returned, just make use of the `arguments` array instead of a written in argument ==> http://jsfiddle.net/3UW8e/
Peter Ajtai
+3  A: 

You should definitely use a regular expression for this.

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

In this case it looks for anything that is a non-digit and replaces with an empty string. The 'g' at the end means "global" so it will replace as many times as it can.

  • ^ This inside the bracket means "not"
  • \d This means "digit"
  • g This means "global"
Stephen
Also `\D` means "not digit"
Pointy
Possibly a stupid question, but if `^` means 'not' and `\d` means 'not digit'...that's a double negative?
David Thomas
@Pointy: I believe it is the "^" that makes the \d negated. (See others have questions like this too) Checkout http://www.w3schools.com/jsref/jsref_obj_regexp.asp
Tommy
Yes, that'll work, but the Javascript regex syntax already has a built-in "not a digit" special character, and it's "\D" (backslash followed by capital 'D').
Pointy
@David Thomas no, "\d" means "digit" and "\D" means "not a digit" - lower-case vs. capital. You use "\D" by itself, without the square brackets (just as one can use "\d" without square brackets to match a digit).
Pointy
@Pointy, *ahhhh* I completely missed the case-change. ...and I only just *bought* these glasses =) (also, that right there is *probably* why I still find regex difficult to grasp...)
David Thomas
Sorry Pointy!!! I missed that too...
Tommy