how to get numeric value from string?

前端 未结 5 911
爱一瞬间的悲伤
爱一瞬间的悲伤 2021-02-08 23:54

This will alert 23.

alert(parseInt(\'23 asdf\'));

But this will not alert 23 but alerts NaN

alert(parseInt(\'asdf 23\'));
         


        
5条回答
  •  谎友^
    谎友^ (楼主)
    2021-02-09 00:11

    function toNumeric(string) {
        return parseInt(string.replace(/\D/g, ""), 10);
    }
    

提交回复
热议问题