Extract a number from a string (JavaScript)

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

    Tried all the combinations cited above with this Code and got it working, was the only one that worked on that string -> (12) 3456-7890

    var str="(12) 3456-7890";
    str.replace( /\D+/g, '');
    

    Result: "1234567890"

    Obs: i know that a string like that will not be on the attr but whatever, the solution is better, because its more complete.

提交回复
热议问题