首頁  >  文章  >  web前端  >  如何使用JavaScript的正規表示式來取得花括號之間的字串?

如何使用JavaScript的正規表示式來取得花括號之間的字串?

PHPz
PHPz轉載
2023-08-23 08:41:071596瀏覽

如何使用JavaScript的正規表示式來取得花括號之間的字串?

我們可以建立一個正規表示式,以便它可以找到所有花括號之間的子字串。之後,我們可以使用exec()或match()方法來提取匹配的子字串。

在本教學中,我們將學習使用JavaScript的正規表示式來取得花括號之間的字串。例如,如果我們有一個字串像'This is a {string} with {curly} braces, we need to extract all substrings that reside between the curly braces. ',我們需要提取所有位於花括號之間的子字串。

使用exec()方法與正規表示式來取得花括號之間的字串

exec()方法通常與正規表示式一起使用,用於尋找符合的子字串。它以數組格式傳回所有符合結果,如果找不到任何符合的情況,則傳回null值。

文法

Users can follow the syntax below to use the exec() method with the regular expression to get all substrings between the curly braces.

var regex = /{([^}]+)}/g;
while (match = regex.exec(originalStr)) {
   results.push(match[1]);
}

在上面的語法中,我們建立了一個正規表示式來尋找大括號之間的子字串。然後,我們遍歷匹配數組並獲取所有匹配的出現。

Example

的中文翻譯為:

範例

‘originalStr’變數包含下面範例中帶有花括號的字串。 regex.exec()方法傳回所有符合子字串的二維陣列。陣列的每個元素都是一個包含兩個字串的陣列。第一個元素是帶有花括號的字串,第二個元素是我們想要提取的不含花括號的字串。

在輸出中,使用者可以觀察到它顯示了「Developers」和「JavaScript」子字串,因為它們都在花括號之間。

<html>
<body>
   <h3>Using the <i> regular expression and exec() method </i> to get string between curly braces in JavaScript</h3>
   <div id = "demo"> </div>
   <script>
      let output = document.getElementById("demo");
      function getStringBetweenBraces() {
         var originalStr = "Hello {Developers}! Let's develop {JavaScript} code!";
         var found = [];
         var regex = /{([^}]+)}/g;
         var results = [];
         let match;
         while (match = regex.exec(originalStr)) {
            results.push(match[1]);
         }
         output.innerHTML = "The original string is " + originalStr + "<br>";
         output.innerHTML += "The substrings between curly braces are " + results + "<br>";
      }
      getStringBetweenBraces();
   </script>
</body>
</html>

使用match()方法與正規表示式來取得大括號之間的字串

match()方法也以陣列格式傳回字串中的所有符合出現。

文法

使用者可以依照下列語法使用match()方法和正規表示式來取得花括號之間的字串

let results = originalStr.match(/{([^}]+)}/g); 

在上面的語法中,originalStr是一個帶有花括號的字串,並且我們將正規表示式作為match()方法的參數傳遞。

Example

的中文翻譯為:

範例

在下面的範例中,match()方法傳回帶有花括號的字串陣列。然而,它不傳回刪除花括號後的字串。使用者可以使用subStr()或substring()方法從傳回的結果中刪除花括號。

<html>
<body>
   <h3>Using the <i> regular expression and match() method </i> to get string between curly braces in JavaScript</h3>
   <div id = "demo"> </div>
   <script>
      let output = document.getElementById("demo");
      function getStringBetweenBraces() {
         var originalStr = "This is {sample} string! This is {original} string!";
         let results = originalStr.match(/{([^}]+)}/g); 
         output.innerHTML = "The original string is " + originalStr + "<br>";
         output.innerHTML += "The substrings between curly braces are " + results + "<br>";
      }
      getStringBetweenBraces();
   </script>
</body>
</html>

使用for迴圈取得大括號之間的字串

我們也可以透過使用for迴圈迭代字串來提取花括號之間的所有子字串。我們可以追蹤花括號的起始索引和結束索引。然後,我們可以使用substring()方法從原始字串中提取子字串。

文法

使用者可以按照以下語法使用for迴圈來提取花括號之間的子字串。

for ( ) {
   if (str[i] == "{")
   start = i;
   if (str[i] == "}") {
      end = i;
      let bracesString = str.substring(start + 1, end);
   }
}   

在上述語法中,我們追蹤花括號的開始和結束索引,並使用substring()方法提取字串。

步驟

第一步 - 使用for迴圈遍歷字串。

第二步驟 - 如果字串中索引為'I'的字元等於'{',則將索引的值儲存在start變數中。

步驟 3 - 如果字串中索引為'I'的字元等於'}',將索引的值儲存在end變數中。

第3.1步 − 使用substring()方法取得在‘end’索引之後的‘start 1’索引之間的子字串。

步驟 3.2 - 將結果子字串推入陣列中。

Example

的中文翻譯為:

範例

在下面的範例中,我們按照上述步驟使用for迴圈提取了花括號之間的字串。然而,這種方法不適用於提取嵌套花括號之間的字串。

<html>
<body>
   <h3>Using the <i> for loop </i> to get string between curly braces in JavaScript</h3>
   <div id = "demo"> </div>
   <script>
      let output = document.getElementById("demo");
      function getStringBetweenBraces() {
         var str = "Hello {Users}! Welcome to {Website}!";
         var start = 0;
         var end = 0;
         let results = [];
         for (var i = 0; i < str.length; i++) {
            if (str[i] == "{") {
               start = i;
            }
            if (str[i] == "}") {
               end = i;
               let bracesString = str.substring(start + 1, end);
               results.push(bracesString);
            }
         }
         output.innerHTML = "The original string is " + str + "<br>";
         output.innerHTML += "The substrings between curly braces are " + results + "<br>";
      }
      getStringBetweenBraces();
   </script>
</body>
</html> 

我們在本教程中學習如何使用正規表示式來提取花括號之間的字串。在第一種方法中,我們使用了具有正規表示式的exec()方法;在第二種方法中,我們使用了具有正規表示式的match()方法。

在第三種方法中,我們使用了for迴圈而不是正規表示式。

以上是如何使用JavaScript的正規表示式來取得花括號之間的字串?的詳細內容。更多資訊請關注PHP中文網其他相關文章!

陳述:
本文轉載於:tutorialspoint.com。如有侵權,請聯絡admin@php.cn刪除