Extract a number from a string (JavaScript)

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

    With Regular Expressions, how to get numbers from a String, for example:

    String myString = "my 2 first gifts were made by my 4 brothers";
    myString = myString .replaceAll("\\D+","");
    System.out.println("myString : " + myString);
    

    the result of myString is "24"

    you can see an example of this running code here: http://ideone.com/iOCf5G

提交回复
热议问题