Page 1 of 1

return only digits from a string

Posted: Sun Jan 03, 2010 2:34 am
by daedalus__
im trying to get the numeric component of a css width and height attribute. but apparently im either regextarded or javascriptarded.


WAIT NEVERMIND

THIS IS RETARDED

Code: Select all

 
    function _trim_chars(string)
    {
        var regex = new RegExp("/\D/g");
        return string.replace(regex, "");
    }
    alert(_trim_chars("f98u2j304fj04"));
 
NO GOOD

Code: Select all

 
function _trim_chars(string)
    {
        var regex = new RegExp("/[^0-9]/g");
        return string.replace(regex, "");
    }
    alert(_trim_chars("f98u2j304fj04"));
 
NO GOOD

Code: Select all

 
    function _trim_chars(string)
    {
        var regex = new RegExp(/[^0-9]/g);
        return string.replace(regex, "");
    }
 
BEANS

im so angry. :|

Re: return only digits from a string

Posted: Sun Jan 03, 2010 2:47 am
by Christopher

Code: Select all

   function _trim_chars(string)
    {
        return string.replace(/[^0-9]/g, "");
    }
    alert(_trim_chars("f98u2j304fj04"));

Re: return only digits from a string

Posted: Sun Jan 03, 2010 4:08 am
by daedalus__
:)

maybe that's why you aren't supposed to encapsulate the regex like a string?

Re: return only digits from a string

Posted: Sun Jan 03, 2010 4:41 am
by kaszu
Just for info, RegExp arguments are ("REGULAR_EXPRESSION", "MODIFIERS")

Code: Select all

function _trim_chars(string)
{
        var regex = new RegExp("[^0-9]", "g");
        return string.replace(regex, "");
}
alert(_trim_chars("f98u2j304fj04"));