jQuery /从字符串中获取数字

时间:2023-02-05 19:39:32

I have a button on my page with a class of comment_like and an ID like comment_like_123456 but the numbers at the end are variable; could be 1 to 1000000.

我的页面上有一个按钮,其中包含一个comment_like类和一个像comment_like_123456这样的ID,但最后的数字是可变的;可能是1到1000000。

When this button is clicked, I need to grab the end number so I can run tasks on other elements with the same suffix.

单击此按钮时,我需要获取结束编号,以便我可以在具有相同后缀的其他元素上运行任务。

Is there an easy way of grabbing this number in jQuery?

有没有一种简单的方法可以在jQuery中获取这个数字?

$('.comment_like').click(function() {
    var element_id = $(this).attr('id');

    // grab number from element ID

    // do stuff with that number

});

7 个解决方案

#1


83  

You can get it like this:

你可以这样得到它:

var suffix = 'comment_like_123456'.match(/\d+/); // 123456

With respect to button:

关于按钮:

$('.comment_like').click(function(){
  var suffix = this.id.match(/\d+/); // 123456
});

#2


10  

In your click handler:

在您的点击处理程序中

var number = $(this).attr('id').split('_').pop();

#3


3  

This is a task for plain-old regular expressions, it has nothing to do with jQuery:

这是普通的正则表达式的任务,它与jQuery无关:

"comment_like_123456".match(/\d+/)

=> ["123456"]

#4


3  

http://jsfiddle.net/hj2nJ/

http://jsfiddle.net/hj2nJ/

var x = 'comment_like_6846511';
var y = '';

for (i = 0; i < x.length; i++)
{
    if ("" + parseInt(x[i]) != "NaN") //if the character is a number
        y = y + x[i];
}

document.write(y);

#5


2  

jQuery is not a magic bullet. Use Javascript!

jQuery不是一个神奇的子弹。使用Javascript!

var temp = "comment_like_123456".split("_")
alert(temp[2])

#6


1  

Just get the id and run it through a regex.

只需获取id并通过正则表达式运行它。

$(mybutton).click(function() {
    var num = parseInt(/^.*\_(\d+)$/.exec(this.id)[1])
});

#7


1  

You can try this. it will extract all number from any type of string.

你可以试试这个。它将从任何类型的字符串中提取所有数字。

var suffix = 'comment_like_6846511';
alert(suffix.replace(/[^0-9]/g,''));

DEMO

DEMO

#1


83  

You can get it like this:

你可以这样得到它:

var suffix = 'comment_like_123456'.match(/\d+/); // 123456

With respect to button:

关于按钮:

$('.comment_like').click(function(){
  var suffix = this.id.match(/\d+/); // 123456
});

#2


10  

In your click handler:

在您的点击处理程序中

var number = $(this).attr('id').split('_').pop();

#3


3  

This is a task for plain-old regular expressions, it has nothing to do with jQuery:

这是普通的正则表达式的任务,它与jQuery无关:

"comment_like_123456".match(/\d+/)

=> ["123456"]

#4


3  

http://jsfiddle.net/hj2nJ/

http://jsfiddle.net/hj2nJ/

var x = 'comment_like_6846511';
var y = '';

for (i = 0; i < x.length; i++)
{
    if ("" + parseInt(x[i]) != "NaN") //if the character is a number
        y = y + x[i];
}

document.write(y);

#5


2  

jQuery is not a magic bullet. Use Javascript!

jQuery不是一个神奇的子弹。使用Javascript!

var temp = "comment_like_123456".split("_")
alert(temp[2])

#6


1  

Just get the id and run it through a regex.

只需获取id并通过正则表达式运行它。

$(mybutton).click(function() {
    var num = parseInt(/^.*\_(\d+)$/.exec(this.id)[1])
});

#7


1  

You can try this. it will extract all number from any type of string.

你可以试试这个。它将从任何类型的字符串中提取所有数字。

var suffix = 'comment_like_6846511';
alert(suffix.replace(/[^0-9]/g,''));

DEMO

DEMO