jQuery - 检查字符串是否包含数值

she*_*nyL 4 javascript jquery

如何通过jquery检查字符串是否包含任何数值?

我搜索了很多例子,但我只能检查一个数字,而不是STRING中的数字.我想找到类似的东西$(this).attr('id').contains("number");

(p/s:我的DOM id将是Large_a(没有数值),Large_a_1(带数值)Large_a_2,等等)

我应该使用什么方法?

Dar*_*rov 8

您可以使用正则表达式:

var matches = this.id.match(/\d+/g);
if (matches != null) {
    // the id attribute contains a digit
    var number = matches[0];
}
Run Code Online (Sandbox Code Playgroud)