title: Retrieve only the Number from an ID field
body: <p>Suppose I have a list like this:</p>
  
  <pre><code>&lt;ul id="items"&gt;
     &lt;li id="item_1_dish"&gt;Item 1&lt;/li&gt;
     &lt;li id="item_2_dish"&gt;Item 2&lt;/li&gt;
     &lt;li id="item_3_dish"&gt;Item 3&lt;/li&gt;
  &lt;/ul&gt;
  </code></pre>
  
  <p>I need to retrieve only the number from the ID. Up till now I have being using the following:</p>
  
  <pre><code>function getNum(element, attrPrefix){
      //set prefix, get number
        var prefix = attrPrefix;
        var num = element.attr("id").substring((prefix.length));
        return num;
  }
  
  function updateDish(field) {
        var num = getNum($(field), "item_");
       //Rest of function ...
       console.log("Number = " + num);// 1_dish, 2_dish, 3_dish
  }
  </code></pre>
  
  <p>How ever this return's 1_dish, 2_dish, 3_dish etc.</p>
  
  <p>I thought using this:</p>
  
  <pre><code>function getNum() {
      var num = /\d+(?=\D*$)/.exec($("element").attr('id'));
      return num;
  }
  
  function updateDish(field) {
        var num = getNum($(field), "item_");
       //Rest of function ...
       console.log("Number = " + num); //null
  }
  </code></pre>
  
  <p>Would return what I needed after reading this post: <a href="https://stackoverflow.com/questions/9330328/jquery-getting-the-number-from-id">Jquery: Getting the number from ID</a> , unfortunatly I get 'null' back instead of the number I require. </p>
  
  <hr>
  
  <p>My ID's actually look like "_bc_inventorybundle_menu_product_0_dish" I just shortened them for example purposes. </p>
  
  <hr>
  
  <p>Thank's to all the quick replies. I went with the accepted answer as it was closest to what I already had. </p>