how to get numeric value from string?

前端 未结 5 918
爱一瞬间的悲伤
爱一瞬间的悲伤 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:06

    You have to use regular expression to extract the number.

    var mixedTextAndNumber= 'some56number';
    var justTheNumber = parseInt(mixedTextAndNumber.match(/\d+/g));
    

提交回复
热议问题