Extract a number from a string (JavaScript)

前端 未结 22 1413
灰色年华
灰色年华 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:19

    For a string such as #box2, this should work:

    var thenum = thestring.replace(/^.*?(\d+).*/,'$1');
    

    jsFiddle:

    • http://jsfiddle.net/dmeku/

提交回复
热议问题