且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

如何在JavaScript中检查字符串是否包含数字?

更新时间:2023-02-26 12:39:07

通过使用以下功能,我们可以测试javascript字符串是否包含数字.在上面的函数中,使用t代替t,我们需要将javascript字符串作为参数传递,然后该函数将返回true或false

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);
}