How to check if a string contains a number in JavaScript?

前端 未结 8 1475
粉色の甜心
粉色の甜心 2021-01-13 18:37

I don\'t get how hard it is to discern a string containing a number from other strings in JavaScript.

Number(\'\') evaluates to 0, while

8条回答
  •  清歌不尽
    2021-01-13 18:49

    By using below function we can test whether a javascript string contains a number or not. In above function inplace of t, we need to pass our javascript string as a parameter, then the function will return either true or false

    function hasNumbers(t)
    {
    var regex = /\d/g;
    return regex.test(t);
    }    
    

提交回复
热议问题