Extract a number from a string (JavaScript)

前端 未结 22 1642
灰色年华
灰色年华 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 06:57

    You can extract numbers from a string using a regex expression:

    let string = "xxfdx25y93.34xxd73";
    let res = string.replace(/\D/g, "");
    console.log(res); 
    

    output: 25933473

提交回复
热议问题