Extract a number from a string (JavaScript)

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

    Try the following: string.replace(/[^0-9]/g, ''); This will delete all non-digit characters, leaving only digits in the string

    function retnum(str) { 
        var num = str.replace(/[^0-9]/g, ''); 
        return parseInt(num,10); 
    }
    

    console.log('abca12bc45qw'.replace(/[^0-9]/g, ''));
    console.log('#box2'.replace(/[^0-9]/g, ''));

    0 讨论(0)
  • 2020-11-22 07:05

    Here's a solt. that checks for no data

    var someStr = 'abc'; // add 123 to string to see inverse
    
    var thenum = someStr.match(/\d+/);
    
    if (thenum != null )
    {
        console.log(thenum[0]);
    }
    else
    {
     console.log('no number');
    }
    
    0 讨论(0)
  • 2020-11-22 07:06

    For this specific example,

     var thenum = thestring.replace( /^\D+/g, ''); // replace all leading non-digits with nothing
    

    in the general case:

     thenum = "foo3bar5".match(/\d+/)[0] // "3"
    

    Since this answer gained popularity for some reason, here's a bonus: regex generator.

    function getre(str, num) {
      if(str === num) return 'nice try';
      var res = [/^\D+/g,/\D+$/g,/^\D+|\D+$/g,/\D+/g,/\D.*/g, /.*\D/g,/^\D+|\D.*$/g,/.*\D(?=\d)|\D+$/g];
      for(var i = 0; i < res.length; i++)
        if(str.replace(res[i], '') === num) 
          return 'num = str.replace(/' + res[i].source + '/g, "")';
      return 'no idea';
    };
    function update() {
      $ = function(x) { return document.getElementById(x) };
      var re = getre($('str').value, $('num').value);
      $('re').innerHTML = 'Numex speaks: <code>' + re + '</code>';
    }
    <p>Hi, I'm Numex, the Number Extractor Oracle.
    <p>What is your string? <input id="str" value="42abc"></p>
    <p>What number do you want to extract? <input id="num" value="42"></p>
    <p><button onclick="update()">Insert Coin</button></p>
    <p id="re"></p>

    0 讨论(0)
  • 2020-11-22 07:07

    You need to add "(/\d+/g)" which will remove all non-number text, but it will still be a string at this point. If you create a variable and "parseInt" through the match, you can set the new variables to the array values. Here is an example of how I got it to work:

        var color = $( this ).css( "background-color" );
        var r = parseInt(color.match(/\d+/g)[0]);
        var g = parseInt(color.match(/\d+/g)[1]);
        var b = parseInt(color.match(/\d+/g)[2]);
    
    0 讨论(0)
  • You should try the following:

    var txt = "#div-name-1234-characteristic:561613213213";
    var numb = txt.match(/\d/g);
    numb = numb.join("");
    alert (numb);​
    

    result

    1234561613213213
    
    0 讨论(0)
  • 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

    0 讨论(0)
提交回复
热议问题