Extract a number from a string (JavaScript)

前端 未结 22 1398
灰色年华
灰色年华 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条回答
  •  礼貌的吻别
    2020-11-22 07:20

    please check below javaScripts, there you can get only number

    var txt = "abc1234char5678#!9";
    var str = txt.match(/\d+/g, "")+'';
    var s = str.split(',').join('');
    alert(Number(s));

    output : 1234567789

提交回复
热议问题