如何通过jquery检查一个字符串是否包含任何数值?
我搜索了很多示例,但我只能找到检查数字的方法,而不是字符串中的数字。我正在尝试查找类似 $(this).attr('id').contains("number");
(p/s:我的 DOM id 将类似于 Large_a
(没有数值),Large_a_1
(有数值),Large_a_2
等)
我应该使用什么方法?
最佳答案
你可以使用正则表达式:
var matches = this.id.match(/\d+/g);
if (matches != null) {
// the id attribute contains a digit
var number = matches[0];
}
关于javascript - jQuery - 检查字符串是否包含数值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6660107/