如何在 JavaScript RegExp 中查詢不在括號之間的字元?


RegExpRegEx 在 JavaScript 中是正則表示式的簡寫形式。正則表示式用於檢查使用者輸入的特定字元、數字或任何其他符號組合的有效性。正則表示式主要用於檢查有效的電子郵件密碼,以驗證正確的使用者登入。

在本教程中,我們將學習如何查詢 JavaScript RegExp 括號中不存在的字元。

語法

以下是用於查詢 JavaScript RegExp 括號中不存在的字元的語法:

[^……]

在上面的語法中,方括號用於 RegExp 的基本語法。而符號^ 用於檢查符號後的字元或字串是否存在於句子中,而用於指示要搜尋的字元。

以下是您可以檢查其是否存在於句子中的專案的列表:

  • [^a] - 它將檢查句子中是否缺少a
  • [^abc] - 它將分別檢查每個字元(a, b, c) 以及句子中組合的(abc) 是否不存在。
  • [^A-Z] - 它將檢查句子中是否存在從大寫A到大寫Z範圍內的任何字元。
  • [^a-z] - 它將檢查句子中是否存在從小寫a到小寫z範圍內的任何字元。
  • [^A-z] - 它將檢查句子中是否存在從大寫A到小寫z範圍內的任何字元。

讓我們用程式碼示例討論所有這些。

演算法

步驟 1 - 在演算法的第一步中,您需要定義一個句子,您將在其中檢查 JavaScript RegExp 括號中不存在的字元。

步驟 2 - 在第二步中,您將根據上面討論的語法,為要檢查的特定字元或字元組合定義正則表示式。

步驟 3 - 在下一步中,您將使用 JavaScript 的內建 match() 函式或方法將原始句子與您定義的正則表示式進行匹配。

步驟 4 - 在最後一步或第四步中,您將顯示在將原始句子與正則表示式匹配後獲得的句子。

示例 1

以下示例將說明如何查詢 JavaScript RegExp 括號中不存在的單個字元。

<!DOCTYPE html> <html> <head> <title>Example - find a character, not between the brackets in JavaScript RegExp</title></head> <body> <h3>Find a character, not between the brackets in JavaScript RegEx</h3> <p>It will do a global search for the characters that are not "t":</p> <p>Original Sentence: Hey there, we are checking for character, not between the brackets of JavaScript RegExp.</p> <button onclick="display()">Check for character</button> <p id="result"></p> <script> function display() { let myStr = "Hey there, we are checking for character, not between the brackets of JavaScript RegExp."; let reg = /[^t]/g; let match = myStr.match(reg); document.getElementById("result").innerHTML = match; } </script> </body> </html>

在上面的示例中,我們搜尋了 JavaScript RegExp 括號中不存在的字元t。因此,它從原始句子中匹配 RegExp 並刪除字元傳遞的所有出現,在本例中,t 將從整個句子中刪除。

示例 2

以下示例將說明如何查詢 JavaScript RegExp 括號中不存在的特定範圍(az 和 A-Z)之間的字元。

<!DOCTYPE html> <html> <body> <h3>Find a character, not between the brackets in JavaScript RegExp</h3> <p>It will do a global search for the characters that are not in range from "a-z" and "A-Z":</p> <p>Original Sentence: <b>Hey there, we are checking for character, not between the brackets of JavaScript RegExp.</b></p> <button onclick="display()">Check for character(in range a to z)</button> <button onclick="display2()">Check for character(in range A to Z)</button> <p id="result"></p> <script> function display() { let myStr = "Hey there, we are checking for character, not between the brackets of JavaScript RegExp."; let reg = /[^a-z]/g; let match = myStr.match(reg); document.getElementById("result").innerHTML = match; } function display2() { let myStr = "Hey there, we are checking for character, not between the brackets of JavaScript RegExp."; let reg = /[^A-Z]/g; let match = myStr.match(reg); document.getElementById("result").innerHTML = match; } </script> </body> </html>

在上面的示例中,我們正在檢查字元是否不在從小寫a到小寫z以及從大寫A到大寫Z的範圍內。如果您單擊按鈕檢查字元(在 a 到 z 的範圍內),它將從原始句子中刪除所有小寫字元。類似地,如果您單擊檢查字元(在 A 到 Z 的範圍內),它將從原始句子中刪除所有大寫字母。

示例 3

以下示例將說明如何同時檢查不同的字元以及這些字元的組合,這些字元不在 JavaScript RegExp 括號中:

<!DOCTYPE html> <html> <body> <h3>Find a character, not between the brackets in JavaScript RegExp</h3> <p>It will do a global search for the characters as well as combination of characters except chareacters in "there"</p> <p>Original Sentence: Hey there, we are checking for character, not between the brackets of JavaScript RegExp.</p> <button onclick="display()">Check for character</button> <p id="result"></p> <script> function display() { let myStr = "Hey there, we are checking for character, not between the brackets of JavaScript RegExp."; let reg = /[^there]/g; let match = myStr.match(reg); document.getElementById("result").innerHTML = match; } </script> </body> </html>

在上面的示例中,我們可以清楚地看到there 從原始句子中刪除,以及字元t、h、e、re 也分別被刪除。

在本教程中,我們已經看到了如何使用三個不同的示例來查詢 JavaScript RegExp 括號中不存在的字元,以檢查不同的字元或單詞。這些示例將幫助您檢查特定輸入欄位(如電子郵件、密碼等)中是否存在任何符合您給定條件的字元。

更新於: 2022-10-31

1K+ 閱讀量

開啟你的 職業生涯

透過完成課程獲得認證

開始學習
廣告

© . All rights reserved.