如何在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()建構函式用一行程式碼來執行此任務。