首頁 > web前端 > html教學 > 在JavaScript的RegExp中找出括號中的數字?

在JavaScript的RegExp中找出括號中的數字?

WBOY
發布: 2023-08-20 18:45:45
轉載
795 人瀏覽過

在JavaScript的RegExp中找出括號中的數字?

在本教學中,我們學習如何使用JavaScript RegExp找到括號中的數字。數字(0-9)的ASCII值從48到57。我們在正規表示式中用[0-9]表示括號中的數字。要找到除所有數字之外的範圍內的數字,我們可以寫出特定的範圍。例如,要找出4和8之間的數字,我們可以在正規表示式模式中寫為[4-8]。現在,我們的目標是使用JavaScript中的RegExp在文字中找到括號內的數字。我們可以按照以下語法來找出括號中的數字。

Syntax

以下是RegExp組[0-9]字元的語法 -

new RegExp("[0-9]") or simply /[0-9]/ 
登入後複製

/[0-9]/, is introduced in ES1. It is fully supported by all browsers. Like, Chrome, IE, Safari, Opera, FireFox and Edge.

RegExp有修飾符,如g,i,m。 "g"用於執行全域匹配,"i"用於執行不區分大小寫的匹配,"m"用於執行多行匹配。

Syntax for /[0-9]/ with modifier like

new RegExp("[0-9]", "g") or simply /[0-9]/g
登入後複製

Algorithm

  • 步驟 1 − 定義一個包含一些數字的字串。
  • STEP 2 − Define the RegExp pattern for digits between brackets.
  • 步驟 3 - 在上述定義的字串上套用match(pattern)函數,以便在字串中尋找括號之間的數字。
  • 第四步 - 顯示結果- 匹配的數字。

讓我們看一些程式範例,以便更清楚地理解。

Example 1

In the program below, we use string match(pattern) to find digits between 1 and 4 in the given string. We use RegExp pattern as /[1-4]/g. The string match() method returns an array of digits in the string.

<!DOCTYPE html>
<html>
<body>
   <h2>Finding digits inside the bracket</h2>
   <p id = "text"></p>
   <p>Digits inside the bracket [1-4] :
   <span id= "result"></span>
   </p>
   <script>
      let myStr = "0127845639Hello";
      document.getElementById("text").innerHTML = myStr;
      let pattern = /[1-4]/g;
      let result = myStr.match(pattern);
      document.getElementById("result").innerHTML = result;
   </script>
</body>
</html>
登入後複製

Here, text is given as 0-9 digits and Hello word. In the pattern, we have given [1-4] only. match() method will search digits from 1 to 4 only. If mentioned digits found in the text, match() method will return an array of existing digits otherwise it will return as null. Let's see another example.

Example 2

In the program below, we take a string with no digits and try to find digits in the string. We use string match(pattern) to find digits between 1 and 4 in the given string. We use the RegExp pattern as / [1-4]/g. See what our output looks like.

<!DOCTYPE html>
<html>
<body>
   <h1>Finding digits inside the bracket</h1>
   <p id= "result"></p>
   <script>
      let text = "567890";
      let pattern = /[1-4]/g;
      let result = text.match(pattern);
      if(result == null){
         document.getElementById("result").innerHTML = "Sorry, there is no digits in text that mentioned in the brackets";
      } else {
         ocument.getElementById("result").innerHTML = result;
      }
   </script>
</body>
</html>
登入後複製

Here, we can observe in the pattern we have mentioned [1-4] but in the text we are given from 5-9 and 0. match() method will return as null because there are no findings. So, if the statement is executed. If input text is given as the first example, then match() will return an array of existing digits and another statement will be executed. Like,

Example 3

<!DOCTYPE html>
<html>
<body>
   <h1>Finding digits inside the bracket</h1>
   <p id= "result"></p>
   <script>
      let text = "0127845639Hello";
      let pattern = /[1-4]/g;
      let result = text.match(pattern);
      if(result == null){
         document.getElementById("result").innerHTML = "Sorry, there is no digits in text that mentioned in the brackets";
      } else {
         document.getElementById("result").innerHTML = "Digit(s) inside the inside the bracket: " + result;
      }
   </script>
</body>
</html>
登入後複製

Now, We will check how to replace word character(s) in a given text. Let’s see an example

Example 4

在括號之間找出並取代數字

在下面的範例中,我們使用split()和join()方法找到並取代1和4之間的數字為空格字元。

<!DOCTYPE html>
<html>
<body>
   <h1>Replace digits inside the bracket</h1>
   <p>After replacing the digits inside the bracket :
   <span id= "result"></span>
   </p>
   <script>
      let text = "0127845639Hello";
      let pattern = /[1-4]/g;
      let result = text.split(pattern).join(" ");
      document.getElementById("result").innerHTML = result;
   </script>
</body>
</html>
登入後複製

Example 5

We will also check to replace the digits inside the bracket using a simpler way. Like,

<!DOCTYPE html>
<html>
<body>
   <h1>Replace digits inside the bracket</h1>
   <p>After replacing the digits inside the bracket :
   <span id= "result"></span>
   </p>
   <script>
      let text = "0127845639Hello";
      let pattern = /[1-4]/g;
      let result = text.replace(pattern , " ");
      document.getElementById("result").innerHTML = result;
   </script>
</body>
</html>
登入後複製

As we discussed, g for global matches. Instead of stopping with the first occurrence, it will look for all the occurrences.

Hope this tutorial will give knowledge on how to find digits inside the brackets using RegExp in JavaScript.

以上是在JavaScript的RegExp中找出括號中的數字?的詳細內容。更多資訊請關注PHP中文網其他相關文章!

相關標籤:
來源:tutorialspoint.com
本網站聲明
本文內容由網友自願投稿,版權歸原作者所有。本站不承擔相應的法律責任。如發現涉嫌抄襲或侵權的內容,請聯絡admin@php.cn
熱門教學
更多>
最新下載
更多>
網站特效
網站源碼
網站素材
前端模板