Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

jQuery - check whether string contains numeric value

How to check whether a string contains any numeric value by jquery?

I search through many examples but I only get the way to check for a number, NOT number in a STRING. I am trying to find something like $(this).attr('id').contains("number");

(p/s: my DOM id will be something like Large_a (without numeric value) , Large_a_1 (with numeric value), Large_a_2, etc.)

What method should I use?

like image 402
shennyL Avatar asked Dec 12 '22 11:12

shennyL


1 Answers

You could use a regular expression:

var matches = this.id.match(/\d+/g);
if (matches != null) {
    // the id attribute contains a digit
    var number = matches[0];
}
like image 127
Darin Dimitrov Avatar answered Dec 26 '22 07:12

Darin Dimitrov