如何在JavaScript中检查字符串是否只包含数字?
在开发过程中,我们可能需要找到只包含数字而不包含任何其他字符的字符串。最简单的检查方法是从字符串中解析数字,并将其长度与原始字符串的长度进行比较。如果两者相同,则表示字符串只包含数字。用户可以使用parseInt()方法从字符串中解析数字。
但是,在本教程中,我们将学习其他方法来检查JavaScript字符串中是否只包含数字。
使用for循环和charCodeAt()方法
我们可以使用for循环迭代字符串。我们可以使用索引值和charCodeAt()方法从字符串中获取每个字符及其ASCII值。之后,我们可以将字符的ASCII值与数字的ASCII值进行比较,并得到该字符是数字字符还是非数字字符的结果。
语法
用户可以按照以下语法检查字符串是否只包含数字。
function isOnlyDigits(string) {
for (let i = 0; i < string.length; i++) {
var ascii = string.charCodeAt(i);
if (ascii < 48 || ascii > 57) {
return false;
}
}
return true;
}
在上面的语法中,我们为每个字符串字符执行charCodeAt()方法。
步骤
步骤1 − 使用for循环迭代字符串。
步骤2 − 使用charCodeAt()方法获取字符串中第i个索引处的字符的ASCII值。
步骤3 − 检查第i个索引处的字符的ASCII值是否在48到57之间。如果不是,则表示它是非数字字符,并返回false。
步骤4 − 如果for循环的迭代完成,并且函数没有找到任何非数字字符,则返回true,表示字符串只包含数字。
示例
在下面的示例中,我们取两个字符串,一个只包含数字,另一个包含混合字符。用户可以观察输出,它会根据字符串只包含数字还是非数字在网页上打印消息。
<html>
<body>
<h3>Using the <i> for-loop and charCodeAt() </i> method to check if string contains only digits</h2>
<div id = "output"> </div>
<script>
let output = document.getElementById("output");
function isOnlyDigits(string) {
for (let i = 0; i < string.length; i++) {
var ascii = string.charCodeAt(i);
if (ascii < 48 || ascii > 57) {
output.innerHTML += "The " + string + " contains non-digits characters also. <br/>";
return false;
}
}
output.innerHTML += "The " + string + " contains only digits characters also. <br/>";
return true;
}
isOnlyDigits("122135567343");
isOnlyDigits("df32d23");
</script>
</body>
</html>
使用Number()构造函数
在JavaScript中,Number是一个对象,我们可以使用它的构造函数来创建它的实例。我们可以将值作为Number()构造函数的参数传递,以使用数字值初始化变量。
我们还可以将数字字符串作为Number()构造函数的参数传递。它将解析数字并返回数字值。如果我们传递包含非数字字符的字符串,它将返回NaN值。
语法
用户可以使用以下语法使用Number constructor()来检查字符串是否只包含数字。
let num = Number(string1);
if (num != NaN) {
// contains non-digit numbers
}else{
// contains only digits.
}
在上面的语法中,string1是一个包含数字和非数字字符的字符串。如果Number()构造函数返回NaN,则字符串包含非数字字符;否则,它将返回数字值。
示例
在下面的示例中,string1只包含数字。我们已将其作为Number()构造函数的参数传递,用户可以看到它在输出中返回实际的数字值而不是NaN。
<html>
<body>
<h3>Using the <i> Number() constructor </i> method to check if string contains only digits</h2>
<div id="output"></div>
<script>
let output = document.getElementById("output");
let string1 = "3433454646";
let num = Number(string1);
if (num != NaN) {
output.innerHTML += "The " + string1 + " Contains only digits! <br/>";
} else {
output.innerHTML += "The " + string1 + " Contains non-digits characters! <br/>";
}
</script>
</body>
</html>
使用正则表达式
在本节中,我们将创建一个正则表达式,我们可以用它来匹配字符串中的非数字字符。如果我们找到任何单个非数字字符的匹配,我们可以说字符串也包含非数字字符。
语法
用户可以按照以下语法使用正则表达式来检查字符串是否只包含数字。
let regex = /\D/;
let bool = regex.test(str);
if (bool) {
// contains non-digits
} else {
// contains only digits
}
test()方法在上面的语法中用于将正则表达式模式与字符串匹配。
如果字符串包含任何非数字字符,则test()方法返回true。
正则表达式解释
\D − 用于匹配任何单个非数字字符。
我们还可以使用“g”标志与正则表达式一起匹配所有非数字字符的出现,但我们只需要知道字符串是否包含单个非数字字符就足够了。
示例
在这个例子中,当用户点击按钮时,isContainsDigits()函数使用不同的字符串参数。此外,用户可以观察使用不同字符串的test()方法的输出。
<html>
<body>
<h3>Using the <i> regular expression </i> method to check if string contains only digits </h3>
<div id = "output"> </div>
<button onclick="isContainsDigits('8954656');isContainsDigits('dfjsdh4354354')">
Click here
</button>
<script>
let output = document.getElementById("output");
let regex = /\D/;
function isContainsDigits(str) {
let bool = regex.test(str);
if (bool) {
output.innerHTML += "The " + str + " Contains non-digits! <br/>";
} else {
output.innerHTML += "The " + str + " Contains only digits characters! <br/>";
}
}
</script>
</body>
</html>
我们学习了三种不同的方法来检查字符串是否只包含数字。用户可以使用Number()构造函数用一行代码来执行此任务。
数据结构
网络
关系数据库管理系统(RDBMS)
操作系统
Java
iOS
HTML
CSS
Android
Python
C语言编程
C++
C#
MongoDB
MySQL
Javascript
PHP