您好,欢迎访问一九零五行业门户网

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() 方法获取字符的 ascii 值,该字符位于字符串中的第 i 个索引处
步骤 3 - 检查索引 ith 处的字符的 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 值。
语法用户可以使用下面的语法来使用 numberconstructor()来检查字符串是否仅包含数字。
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() 构造函数通过一个线性代码来执行任务。
以上就是javascript 中如何检查字符串是否只包含数字?的详细内容。
其它类似信息

推荐信息