title: Javascript regex: How to extract an "id" from a string?
body: <p>I have this string: <code>comment_1234</code> </p>
  
  <p>I want to extract the <code>1234</code> from the string. How can I do that?</p>
  
  <p><strong>Update:</strong> I can't get any of your answers to work...Is there a problem with my code? The alert never gets called:</p>
  
  <pre><code>var nameValue = dojo.query('#Comments .Comment:last-child &gt; a[name]').attr('name');
  alert('name value: ' + nameValue); // comment_1234
  var commentId = nameValue.split("_")[1];
  // var commentId = nameValue.match(/\d+/)[0];
  // var commentId = nameValue.match(/^comment_(\d+)/)[1];
  alert('comment id: ' + commentId); //never gets called. Why?
  </code></pre>
  
  <h2>Solution:</h2>
  
  <p>I figured out my problem...for some reason it looks like a string but it wasn't actually a string, so now I am casting <code>nameValue</code> to a string and it is working.</p>
  
  <pre><code>var nameValue = dojo.query('#Comments .Comment:last-child &gt; a[name]').attr('name'); //comment_1234
  var string = String(nameValue); //cast nameValue as a string
  var id = string.match(/^comment_(\d+)/)[1]; //1234
  </code></pre>