ES6 中的 RegExp 解釋?


ES6 是 2015 年釋出的最新 JavaScript 版本。我們將學習 RegExp(即正則表示式)的所有功能,這些功能包含在 Es6 中。

什麼是正則表示式以及它的用法

正則表示式是一個表示特定搜尋模式的字串,包含不同的字元,例如數字、字母和特殊字元。

讓我們瞭解正則表示式的即時用法。

我們希望大家過去都填寫過一些表單,並且見過一些特定的表單欄位驗證錯誤。例如,如果您未輸入包含“@”字元的電子郵件或輸入了非 10 位數的手機號碼,則會顯示錯誤。因此,我們可以使用正則表示式進行表單欄位輸入驗證。

此外,我們還可以使用正則表示式在字串中查詢特定子字串,並將其替換為新字串。

正則表示式可用於密碼驗證,以確保密碼強度。

正則表示式還有很多其他用途,如上所述。

語法

我們可以使用以下兩種不同的方式定義正則表示式。

  • 字面量表示法 - 使用者需要在兩個正斜槓之間定義搜尋模式,以將搜尋模式表示為正則表示式。

myImage.style.display = "block";
  • 建構函式 - 我們可以使用帶有 new 關鍵字的 RegExp() 建構函式來定義正則表示式物件。使用者可以按照以下語法使用建構函式定義正則表示式。

var pattern = new RegExp(search_pattern, attributes);

屬性

我們可以使用以下多個屬性的組合。

屬性

描述

G

用於字串中的全域性匹配。

I

忽略字串中的大小寫。

M

用於多行匹配。

U

U 代表 Unicode,它允許將 RegExp 視為 Unicode 程式碼點的序列。

Y

用於從最後一個索引開始匹配。

構建正則表示式

我們可以使用不同的特殊字元來構建正則表示式,我們將在下面的示例中瞭解這些字元。

方括號

我們可以使用方括號在正則表示式模式中定義字元範圍。

方括號表示法

描述

[abc…]

在搜尋字串中搜索方括號中的任何單個字元。

[^abc…]

搜尋不在方括號中的任何單個字元。

[DL]

搜尋 D 和 L 之間的任何單個大寫字元。

[dl]

搜尋 D 和 L 之間的任何單個小寫字元。

[09]

搜尋 0 和 9 之間的任何單個數字。

示例 1

在此示例中,我們使用修飾符“g”和方括號表示法建立了一個模式,並將該模式與訊息字串進行了匹配。您可以觀察到它只從 JavaScript 中返回“Script”,因為我們沒有使用“I”修飾符忽略大小寫。

<html>
<body>
   <h2> Using the bracket notations in the regular expression </h2>
   <p id="output"> </p>
</body>
   <script>
      let output = document.getElementById("output");
      let message ="TutorialsPoint is a great website to learn JavaScript and Typescript.";
      // defining the regular expression pattern
      var pattern = new RegExp("Script", "g");
      // matching the pattern in string
      var script_match1 = message.match(pattern);
      output.innerHTML +=
      "All matches without case ignorance: " + script_match1 + "<br/>";
   </script>
</html>

示例 2

在此示例中,我們找到所有不區分大小寫的匹配項。您可以觀察到我們使用“I”修飾符忽略了大小寫。

<html>
<body>
   <h2> Using the bracket notations in the regular expression </h2>
   <p id="output"> </p>
   </body>
   <script>
      let output = document.getElementById("output");
      let message ="TutorialsPoint is a great website to learn JavaScript and Typescript.";
      // Matching with case ignorance
      pattern = new RegExp("Script", "gi");
      script_match1 = message.match(pattern);
      output.innerHTML +=
      "All matches with case ignorance:" + script_match1 + "<br/>";
   </script>
</html>

示例 3

在此示例中,我們匹配“a”和“c”之間的字元。

<html>
<body>
   <h2> Using the bracket notations in the regular expression </h2>
   <p id="output"> </p>
   </body>
   <script>
      let output = document.getElementById("output");
      let message ="TutorialsPoint is a great website to learn JavaScript and Typescript.";
      // matching from a particular range
      pattern = new RegExp("[a-c]", "g");
      script_match1 = message.match(pattern);
      output.innerHTML +=
      "All matches between a to c: " + script_match1 + "<br/>";
   </script>
</html>

量詞

我們可以使用量詞來匹配給定字串中特定數量的字元。我們可以使用不同的特殊字元來建立不同型別的量詞。

量詞

描述

C+

搜尋包含一個或多個 C 的字串。

C*

搜尋包含零個或多個 C 的字串。

C{M}

搜尋包含正好 M 個 C 的字串。

C{M, N}

搜尋包含 M 和 N 之間總數為 C 的字串。

C{M, }

搜尋包含至少 M 個 C 的字串。

C$

搜尋以 C 結尾的字串。

^C

搜尋以 C 開頭的字串。

示例

在此示例中,我們在建立正則表示式時使用了不同的量詞,並將訊息字串與正則表示式模式進行了匹配。

<html>
<body>
   <h2> Using the <i> Quantifiers </i> in the regular expression </h2>
   <p id="output"> </p>
</body>
   <script>
      let output = document.getElementById("output");
      let message =
      "TutorialsPoint is a great website to learn JavaScript and Typescript.";
      // defining the regular expression pattern
      var pattern = new RegExp("a+", "g");

      // matching the pattern in string
      var script_match1 = message.match(pattern);
      output.innerHTML +=
      "All matches which contain at least one a: " +
      script_match1 +
      "<br/>";

      // Matching with case ignorance
      pattern = new RegExp("^t", "gi");
      script_match1 = message.match(pattern);
      output.innerHTML +=
      "All matches which starts with t: " + script_match1 + "<br/>";
   </script>
</html>

字面量字元

字面量字元 我們可以使用字面量字元來表示正則表示式中的跳脫字元。

字面量

描述

\0

表示 NULL 字元。

\t

表示製表符


\n

表示換行符。

\v

表示垂直製表符。

\r

示例

用於使用十六進位制數 xxxx 指定 Unicode 字元 u。

<html>
<body>
   <h2> Using the <i> Literal characters </i> in the regular expression </h2>
   <p id="output"></p>
   </body>
   <script>
      let output = document.getElementById("output");
      let message =
      "TutorialsPoint is a great website to learn JavaScript and Typescript.";
      // defining the regular expression pattern
      var pattern = new RegExp("\T", "g");

      // matching the pattern in string
      var script_match1 = message.replace(pattern, "\p");
      output.innerHTML +=
      "After replacing the T in the string with p is : <br>" +
      script_match1 +
      "<br/>";
   </script>
</html>

在此示例中,我們使用了“\t”字面量來轉義製表符,並將訊息字串中的所有“T”字元替換為“p”。

元字元

描述

元字元

\s

指定空格。

\S

指定非空格。

\w

指定單詞字元。

\W

指定非單詞字元。

\d

指定十進位制數字。

\D

指定非十進位制數字。

更新於: 2022-12-29

143 次檢視

開啟你的 職業生涯

透過完成課程獲得認證

立即開始
廣告

© . All rights reserved.