Extract a number from a string (JavaScript)

前端 未结 22 1389
灰色年华
灰色年华 2020-11-22 06:38

I have a string in JavaScript like #box2 and I just want the 2 from it.

I tried:

var thestring = $(this).attr(\'href\');
var          


        
22条回答
  •  慢半拍i
    慢半拍i (楼主)
    2020-11-22 06:55

    If someone need to preserve dots in extracted numbers:

    var some = '65,87 EUR';
    var number = some.replace(",",".").replace(/[^0-9&.]/g,'');
    console.log(number); // returns 65.87
    

提交回复
热议问题