admin管理员组

文章数量:1357294

I have this string: ment_1234

I want to extract the 1234 from the string. How can I do that?

Update: I can't get any of your answers to work...Is there a problem with my code? The alert never gets called:

var nameValue = dojo.query('#Comments .Comment:last-child > a[name]').attr('name');
alert('name value: ' + nameValue); // ment_1234
var mentId = nameValue.split("_")[1];
// var mentId = nameValue.match(/\d+/)[0];
// var mentId = nameValue.match(/^ment_(\d+)/)[1];
alert('ment id: ' + mentId); //never gets called. Why?

Solution:

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 nameValue to a string and it is working.

var nameValue = dojo.query('#Comments .Comment:last-child > a[name]').attr('name'); //ment_1234
var string = String(nameValue); //cast nameValue as a string
var id = string.match(/^ment_(\d+)/)[1]; //1234

I have this string: ment_1234

I want to extract the 1234 from the string. How can I do that?

Update: I can't get any of your answers to work...Is there a problem with my code? The alert never gets called:

var nameValue = dojo.query('#Comments .Comment:last-child > a[name]').attr('name');
alert('name value: ' + nameValue); // ment_1234
var mentId = nameValue.split("_")[1];
// var mentId = nameValue.match(/\d+/)[0];
// var mentId = nameValue.match(/^ment_(\d+)/)[1];
alert('ment id: ' + mentId); //never gets called. Why?

Solution:

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 nameValue to a string and it is working.

var nameValue = dojo.query('#Comments .Comment:last-child > a[name]').attr('name'); //ment_1234
var string = String(nameValue); //cast nameValue as a string
var id = string.match(/^ment_(\d+)/)[1]; //1234
Share Improve this question edited Mar 5, 2010 at 18:55 Andrew asked Mar 5, 2010 at 17:47 AndrewAndrew 239k195 gold badges531 silver badges718 bronze badges
Add a ment  | 

3 Answers 3

Reset to default 6
someString.match(/\d+/)[0]; // 1234

Or, to specifically target digits following "ment_":

someString.match(/^ment_(\d+)/)[1]; // 1234
function ExtractId(str){
    var params = str.split('_');
    return params[params.length - 1];
}

var id = ExtractId('ment_1234');

var tesst = "WishID=1List"
var test = tesst.match(/WishID=(.*)List/);
alert (test[1]);

if(test[1]==""){
  alert('Empty');
}

本文标签: Javascript regex How to extract an quotidquot from a stringStack Overflow