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
3 Answers
Reset to default 6someString.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
版权声明:本文标题:Javascript regex: How to extract an "id" from a string? - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1744075581a2586684.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论