Regex that matches integers only

前端 未结 10 572
执笔经年
执笔经年 2020-11-30 09:56

I\'m currently using the pattern: \\b\\d+\\b, testing it with these entries:

numb3r 
2
3454 
3.214
test

I only want it to catc

10条回答
  •  甜味超标
    2020-11-30 10:15

    I would add this as a comment to the other good answers, but I need more reputation to do so. Be sure to allow for scientific notation if necessary, i.e. 3e4 = 30000. This is default behavior in many languages. I found the following regex to work:

    /^[-+]?\d+([Ee][+-]?\d+)?$/;
    //          ^^              If 'e' is present to denote exp notation, get it
    //             ^^^^^          along with optional sign of exponent
    //                  ^^^       and the exponent itself
    //        ^            ^^     The entire exponent expression is optional
    

提交回复
热议问题