I am trying to work with the following string: comment_1234
My goal is to extract the number 1234
from this string. How can I achieve this?
Update: Despite attempting various solutions provided, none seem to be working for me. Could there be an issue with my code? The alert function does not trigger as expected:
var nameValue = dojo.query('#Comments .Comment:last-child > 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?
Solution:
After some investigation, I identified the root of my issue...the variable nameValue
appeared to be a string but was actually something else. By explicitly casting nameValue
as a string, I was able to resolve the problem.
var nameValue = dojo.query('#Comments .Comment:last-child > a[name]').attr('name'); //comment_1234
var string = String(nameValue); //cast nameValue as a string
var id = string.match(/^comment_(\d+)/)[1]; //1234